Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements.
// Outputs F because A is replaced with B, then B is replaced with C, and so on...
// Finally E is replaced with F, because of left to right replacements.
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);
Answer from Rakesh Tembhurne on Stack OverflowPHP
php.net › manual › en › function.str-replace.php
PHP: str_replace - Manual
So instead of starting from "A" and ending with "E": <?php $search = array('A', 'B', 'C', 'D', 'E'); $replace = array('B', 'C', 'D', 'E', 'F'); // replaces A to B, B to C, C to D, D to E, E to F (makes them all F) // start from "E" and end with "A": $search = array('E', 'D', 'C', 'B', 'A'); $replace = array('F', 'E', 'D', 'C', 'B'); // replaces E to F, D to E, C to D, B to C, A to B (prevents from // multiple replacements of already replaced values) ?> So basically start from the "end" and put the replacements in an order where the "replaced value" won't equal a value that exists later in the "search array". ... This strips out horrible MS word characters.
W3Schools
w3schools.com › php › func_string_str_replace.asp
PHP str_replace() Function
If find is an array and replace is a string, the replace string will be used for every find value · Note: This function is case-sensitive. Use the str_ireplace() function to perform a case-insensitive search. Note: This function is binary-safe. ... <?php $arr = array("blue","red","green","yellow"); print_r(str_replace("red","pink",$arr,$i)); echo "Replacements: $i"; ?> Try it Yourself »
php - str_replace() is corrupting the result by replacing previously replaced strings - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I'm having some troubles with the PHP function str_replace when using arrays. More on stackoverflow.com
Replace string in an array with PHP - Stack Overflow
Relevant pages with better clarity: Replace substring in column values of a 2d array and changing keys Recursively change keys in array ... Even "How can I do that on keys of array?" is ambiguous. Do you want to change the keys or only change values for specific keys? ... Save this answer. ... Show activity on this post. ... When I try this with a decoded JSON array I get an error PHP Catchable fatal error: Object of class stdClass could not be converted to string ... More on stackoverflow.com
PHP str_replace array - Stack Overflow
Sign up to request clarification or add additional context in comments. ... You can simply do it echo implode(',',array_map(function($val){ return "?";},$setValues)); 2015-10-07T08:47:38.7Z+00:00 ... function myfunction($num) { return '?'; } $setValues = array("test1", "test2", "testurl"); $setValues = array_map("myfunction",$setValues); $change = join("','", $setValues); $done = str_replace... More on stackoverflow.com
php - str_replace() with associative array - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. ... Copy$array_from = array ('from1', 'from2'); $array_to = array ('to1', 'to2'); $text = str_replace ($array_from, $array_to, $text); More on stackoverflow.com
PHP Tutorial
phptutorial.net › home › php tutorial › php str_replace
PHP str_replace - PHP Tutorial
April 7, 2025 - Summary: in this tutorial, you’ll learn how to use the PHP str_replace() function to replace all occurrences of a substring with a new string · The PHP str_replace() function returns a new string with all occurrences of a substring replaced with another string.
Arievisser
arievisser.com › blog › replace-multiple-items-from-string-text-in-php-with-array
Replace Multiple Items From String Text in PHP With Array | Arie Visser
str_replace( array|string $search, array|string $replace, string|array $subject, int &$count = null ): string|array
BCCNsoft
doc.bccnsoft.com › docs › php-docs-7-en › function.str-replace.html
Replace all occurrences of the search string with the replacement string
This function returns a string or an array with the replaced values. ... <?php // Provides: <body text='black'> $bodytag = str_replace("%body%", "black", "<body text='%body%'>"); // Provides: Hll Wrld f PHP $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); $onlyconsonants = ...
Top answer 1 of 5
64
Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements.
// Outputs F because A is replaced with B, then B is replaced with C, and so on...
// Finally E is replaced with F, because of left to right replacements.
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);
2 of 5
50
str_replace with arrays just performs all the replacements sequentially. Use strtr instead to do them all at once:
$new_message = strtr($message, 'lmnopq...', 'abcdef...');
Top answer 1 of 8
102
Why not just use str_replace without a loop?
$array = array('foobar', 'foobaz');
$out = str_replace('foo', 'hello', $array);
2 of 8
30
$array = array_map(
function($str) {
return str_replace('foo', 'bar', $str);
},
$array
);
But array_map is just a hidden loop. Why not use a real one?
foreach ($array as &$str) {
$str = str_replace('foo', 'bar', $str);
}
That's much easier.
Zend
php-legacy-docs.zend.com › manual › php5 › en › function.str-replace
Replace all occurrences of the search string with the replacement string
If you don't need fancy replacing rules (like regular expressions), you should use this function instead of preg_replace(). If search and replace are arrays, then str_replace() takes a value from each array and uses them to search and replace on subject. If replace has fewer values than search, ...
A Star Trek Timeline
macs.hw.ac.uk › ~hwloidl › docs › PHP › function.str-replace.html
str_replace
As of PHP 4.0.5, every parameter in str_replace() can be an array.
The Electric Toolbox
electrictoolbox.com › home › how to use an associative array with php’s str_replace function
How to use an associative array with PHP's str_replace function | The Electric Toolbox Blog
January 2, 2020 - If the search parameter is an array and the replace parameter a string, everything matching each of the array items in the source string will be replaced with the replace parameter.
W3Schools
www-db.deis.unibo.it › courses › TW › DOCS › w3schools › php › func_string_str_replace.asp.html
PHP str_replace() Function
If find is an array and replace is a string, the replace string will be used for every find value · Note: This function is case-sensitive. Use the str_ireplace() function to perform a case-insensitive search. Note: This function is binary-safe. ... <?php $arr = array("blue","red","green","yellow"); print_r(str_replace("red","pink",$arr,$i)); echo "Replacements: $i"; ?> Run example »
Top answer 1 of 3
7
use array_map() with callback
$setValues = array("test1", "test2", "testurl");
$change = array_map(function($val) { return "?"; }, $setValues);
$change = join(",", $change);
echo $change;// outputs => ?,?,?
2 of 3
0
This will work for you-
function myfunction($num)
{
return '?';
}
$setValues = array("test1", "test2", "testurl");
$setValues = array_map("myfunction",$setValues);
$change = join("','", $setValues);
$done = str_replace("", "?", $change);
Top answer 1 of 5
63
$text = strtr($text, $array_from_to)
By the way, that is still a one dimensional "array."
2 of 5
40
$array_from_to = array (
'from1' => 'to1',
'from2' => 'to2'
);
$text = str_replace(array_keys($array_from_to), $array_from_to, $text);
The to field will ignore the keys in your array. The key function here is array_keys.