ID:               47232
 User updated by:  michael dot moussa at gmail dot com
 Reported By:      michael dot moussa at gmail dot com
 Status:           Bogus
 Bug Type:         Strings related
 Operating System: *
 PHP Version:      5.2.8
 New Comment:

Jani,

The documentation says "This function returns a string or an array 
with all occurrences of search in subject replaced with the given 
replace value."

Refer to the following example:

$subject = 'A';

echo str_replace( array('A','B','C','D'), array('B','C','D','E'), 
$subject);

The result is 'E'.  If str_replace() is replacing occurrences in 
*$subject*, then how do we end up with 'E' if 'D' doesn't exist in 
$subject?  The result SHOULD be 'B'.  $subject is 'A', so the first 
replacement replaces 'A' with 'B'.  The second replacement, as the 
documentation states, should check for occurrences of 'B' within 
$subject, *NOT* within str_replace('A', 'B', $subject) - and so on.

If this result is not a bug, then the documentation should be updated 
to indicate that subsequent replacements are done on the result of 
the previous replacement operation.

Thanks


Previous Comments:
------------------------------------------------------------------------

[2009-01-28 22:50:33] [email protected]

I mistakenly _deleted_ your comment in the manual when I was supposed 
to edit it and add a note about preg_replace() there as well. Sorry 
for the inconvenience, but can you readd it? (that stupid comment 
moderator thing should ask "are you sure" before actually deleting 
stuff..)


------------------------------------------------------------------------

[2009-01-28 22:47:05] [email protected]

Maybe this is a bit confusing but I do understand it exactly like 
that:
"If search or replace are arrays, their elements are processed first 
to last."

So in: 
a b c d e 
b c d e f

1st pass: b b c d e
2nd pass: c c c d e
3rd pass: d d d d e
4th pass: e e e e e
5th pass: f f f f f

If it did not work like it does, it would not do what it's expected 
to do. Use preg_replace() instead with proper regexps. str_replace() 
is the quick'n' dirty method..


------------------------------------------------------------------------

[2009-01-28 18:56:10] michael dot moussa at gmail dot com

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 this bug report at http://bugs.php.net/?id=47232&edit=1

Reply via email to