#47232 [NEW]: str_replace factors previous replacements into process = unexpected output

From: Date: Wed, 28 Jan 2009 18:56:14 +0000
Subject: #47232 [NEW]: str_replace factors previous replacements into process = unexpected output
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-133272@lists.php.net to get a copy of this message
From: michael dot moussa at gmail dot com Operating system: Irrelevant PHP version: 5.2.8 PHP Bug Type: Output Control Bug description: str_replace factors previous replacements into process = unexpected output Description: ------------ When $search and $replace are arrays, str_replace factors previous replacements into the process and produces unexpected input. Reproduce code: --------------- <?php $search = array('A', 'B', 'C', 'D', 'E'); $replace = array('B', 'C', 'D', 'E', 'F'); echo str_replace($search, $replace, 'ABCDE'); ?> Expected result: ---------------- Each letter in the string 'ABCDE' should be replaced by the letter that follows it in the alphabet, that is, A should be replaced with B, B should be replaced with C, etc... The expected result is 'BCDEF' Actual result: -------------- The actual result is 'FFFFF'. The documentation does not indicate that $subject is continuously updated as the process moves along. A more practical example: echo str_replace( array(" ", "&"), array("&nbsp", "&amp;"), "Hello & goodbye!" ); This should replace "Hello & goodbye!" with "Hello&nbsp;&amp;&nbsp;goodbye!" but it actually results in "Hello&amp;nbsp&amp;&amp;nbspgoodbye!", as the original replacement of " " with "&nbsp;" introduced an additional ampersand. -- Edit bug report at http://bugs.php.net/?id=47232&edit=1 -- Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=47232&r=trysnapshot52 Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=47232&r=trysnapshot53 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=47232&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=47232&r=fixedcvs Fixed in CVS and need be documented: http://bugs.php.net/fix.php?id=47232&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=47232&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=47232&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=47232&r=needscript Try newer version: http://bugs.php.net/fix.php?id=47232&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=47232&r=support Expected behavior: http://bugs.php.net/fix.php?id=47232&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=47232&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=47232&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=47232&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=47232&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=47232&r=dst IIS Stability: http://bugs.php.net/fix.php?id=47232&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=47232&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=47232&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=47232&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=47232&r=mysqlcfg

« previous php.bugs (#133272) next »