note 69573 added to function.str-replace
| From: | ae at instinctive dot de | Date: | Mon, 11 Sep 2006 14:22:49 +0000 |
| Subject: | note 69573 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-117014@lists.php.net to get a copy of this message | ||
CAUTION: This function will NOT do what you expect in many cases when you use arrays as both search
and replace parameters. I found this out the hard way in a production website...
Consider the following code:
$a = "abcd";
$a = str_replace(array("a", "b", "c", "d"),
array("albert ", "belinda ", "charlie ", "diana "), $a);
echo $a;
You would expect it to output "albert belinda charlie diana ", but it outputs:
"albelindiana a ert belindiana a charlie diana "
str_replace() simply executes itself for each array element, using the *already modified* string as
input. This should be clearly noted in the manual.
----
Server IP: 212.124.37.9
Probable Submitter: 82.139.254.221
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/69573
Del: integrated -- https://master.php.net/note/delete/69573/integrated
Del: useless -- https://master.php.net/note/delete/69573/useless
Del: bad code -- https://master.php.net/note/delete/69573/bad+code
Del: spam -- https://master.php.net/note/delete/69573/spam
Del: non-english -- https://master.php.net/note/delete/69573/non-english
Del: in docs -- https://master.php.net/note/delete/69573/in+docs
Del: other reasons-- https://master.php.net/note/delete/69573
Reject -- https://master.php.net/note/reject/69573
Search -- https://master.php.net/manage/user-notes.php