note 69573 deleted from function.str-replace by philip

From: Date: Mon, 26 Feb 2007 07:46:51 +0000
Subject: note 69573 deleted from function.str-replace by philip
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-123889@lists.php.net to get a copy of this message
Note Submitter: ae at instinctive dot de Reason: integrated ---- 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.

« previous php.notes (#123889) next »