note 88541 added to function.str-replace
| From: | michael dot moussa at gmail dot com | Date: | Wed, 28 Jan 2009 16:47:12 +0000 |
| Subject: | note 88541 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-149893@lists.php.net to get a copy of this message | ||
As previous commentators mentioned, there is a bug with str_replace(...) when $search and $replace
are arrays in that it factors previous replacements into the process rather than operating solely on
the original string as is expected.
Sample:
<?php
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
echo str_replace($search, $replace, 'ABCDE'); // output: 'FFFFFF'
?>
In the above code, the $search and $replace should replace each occurrence in the $subject with the
next letter in the alphabet. The expected output for this sample is 'BCDEF'; however, the
actual output is 'FFFFF'.
The following function utilizes array_combine and strtr to produce the expected output, and I
believe it is the most efficient way to work around this bug.
<?php
/**
* String replacement function designed to work around the bug in str_replace(...) when working with
* arrays. In str_replace(...), values that did not exist in the original string (but were put there
* by previous replacements) will be replaced continuously and result in unexpected output.
*
* @param array $search list of strings or characters that need to be replaced
* @param array $replace list of strings or characters that will replace the corresponding values in
$search
* @param string $subject the string on which this operation is being performed
*
* @return string $subject with all substrings in the $search array replaced by the values in the
$replace array
*/
function stro_replace($search, $replace, $subject)
{
return strtr( $subject, array_combine($search, $replace) );
}
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
echo stro_replace($search, $replace, 'ABCDE'); // output: 'BCDEF'
?>
Another example:
<?php
$search = array('ERICA', 'AMERICA');
$replace = array('JON', 'PHP');
$subject = 'MIKE AND ERICA LIKE AMERICA';
// We want to replace the name "ERICA" with "JON" and the word
"AMERICA" with "PHP"
echo str_replace($search, $replace, $subject); // output: "MIKE AND JON LIKE AMJON", which
is not correct
echo stro_replace($search, $replace, $subject); // output: "MIKE AND JON LIKE PHP", which
is correct
?>
----
Server IP: 64.71.164.2
Probable Submitter: 216.177.196.194
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/88541
Del: integrated -- https://master.php.net/note/delete/88541/integrated
Del: useless -- https://master.php.net/note/delete/88541/useless
Del: bad code -- https://master.php.net/note/delete/88541/bad+code
Del: spam -- https://master.php.net/note/delete/88541/spam
Del: non-english -- https://master.php.net/note/delete/88541/non-english
Del: in docs -- https://master.php.net/note/delete/88541/in+docs
Del: other reasons-- https://master.php.net/note/delete/88541
Reject -- https://master.php.net/note/reject/88541
Search -- https://master.php.net/manage/user-notes.php