From: dhoepelman at gmail dot com Operating system: Irrelevent PHP version: Irrelevant Package: *General Issues Bug Type: Feature/Change Request Bug description:str_replace with array key => value paris
Description: ------------ A common pattern in PHP is to see str_replace being used with array_keys and array_values to replace in a string according to the keys and values of an associative array. See comment of Wes Foster: http://php.net/str_replace#95198 It would be nice if PHP would provide a shorthand function for this, which will probably also provide an speed bonus. Could be implementend as a seperate function or as different arguments, similar to strstr. Test script: --------------- /** * Replace keys of associative array in string with values of associative array * @param string[] $replacing Key => value pairs of replacements * @param string $subject The haystack * @see str_replace() **/ function str_replace_assoc($replacing, $subject) { return str_replace(array_keys($replacing), array_values($replacing), $subject); } // Provides: You should eat pizza, beer, and ice cream every day $phrase = "You should eat fruits, vegetables, and fiber every day."; $replacements = ["fruits" => "pizza", "vegetables" => "beer", "fiber" => "ice cream"]; echo str_replace($replacements, $phrase); Expected result: ---------------- You should eat pizza, beer, and ice cream every day -- Edit bug report at https://bugs.php.net/bug.php?id=65020&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=65020&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=65020&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=65020&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=65020&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=65020&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=65020&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=65020&r=needscript Try newer version: https://bugs.php.net/fix.php?id=65020&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=65020&r=support Expected behavior: https://bugs.php.net/fix.php?id=65020&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=65020&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=65020&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=65020&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65020&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=65020&r=dst IIS Stability: https://bugs.php.net/fix.php?id=65020&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=65020&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=65020&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=65020&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=65020&r=mysqlcfg