note 88545 added to function.str-replace
| From: | michael dot moussa at gmail dot com | Date: | Wed, 28 Jan 2009 18:35:31 +0000 |
| Subject: | note 88545 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-149900@lists.php.net to get a copy of this message | ||
As previous commentators mentioned, when $search contains values that occur earlier in $replace,
str_replace will factor those previous replacements into the process rather than operating solely on
the original string. If this is not your intention when using str_replace, you may get unexpected
output.
Example:
<?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 perform the desired string replacement without prior
replacements affecting the final result.
<?php
/**
* When using str_replace(...), values that did not exist in the original string (but were put there
by previous
* replacements) will be replaced continuously. This string replacement function is designed replace
the values
* in $search with those in $replace while not factoring in prior replacements. Note that this
function will
* always look for the longest possible match first and then work its way down to individual
characters.
*
* The "o" in "stro_replace" represents "original", indicating that the
function operates only on the original string.
*
* @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
?>
Please e-mail me with questions or comments.
----
Server IP: 69.147.83.197
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/88545
Del: integrated -- https://master.php.net/note/delete/88545/integrated
Del: useless -- https://master.php.net/note/delete/88545/useless
Del: bad code -- https://master.php.net/note/delete/88545/bad+code
Del: spam -- https://master.php.net/note/delete/88545/spam
Del: non-english -- https://master.php.net/note/delete/88545/non-english
Del: in docs -- https://master.php.net/note/delete/88545/in+docs
Del: other reasons-- https://master.php.net/note/delete/88545
Reject -- https://master.php.net/note/reject/88545
Search -- https://master.php.net/manage/user-notes.php