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 Overflow
🌐
PHP
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 »
Discussions

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
🌐 stackoverflow.com
April 14, 2017
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
🌐 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
🌐 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
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-str_replace-function
PHP str_replace() Function - GeeksforGeeks
November 30, 2021 - The str_replace() is a built-in function in PHP and is used to replace all the occurrences of the search string or array of search strings by replacement string or array of replacement strings in the given string or array respectively.
🌐
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
🌐
Benjamin Crozat
benjamincrozat.com › home › blog › php str_replace(): examples, gotchas, and alternatives
PHP str_replace(): examples, gotchas, and alternatives
August 15, 2026 - The useful part is knowing when ... instead of stopping at one line of syntax. str_replace() replaces every occurrence of one exact string with another exact string....
🌐
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 = ...
Find elsewhere
🌐
Pi My Life Up
pimylifeup.com › home › how to use the str_replace function in php
How to use the str_replace Function in PHP - Pi My Life Up
April 17, 2022 - When using an array of replacements, the order is essential. For example, “search1“, will be replaced with “replacement1“. Alternatively, you can pass in a string, and PHP will replace all elements with that text ("replacement").
🌐
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, ...
🌐
Udemy
blog.udemy.com › home › it & development › web development › how to use the php str_replace function to find and replace strings
How to Use the PHP STR_REPLACE Function - Udemy Blog
April 14, 2026 - The PHP `str_replace()` function searches a string or array and replaces every matching instance with a new value. This article covers its syntax, parameters, case sensitivity, empty-string replacements, and array usage.
🌐
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.
🌐
Phpmentoring
phpmentoring.org › blog › php-str-replace
PHP Str_Replace() Function - How To Replace Characters In A String In PHP
The function returns either a string or an array with the replacement values. If arrays were used for subjectValue then we return an array of each element the function performed a search and replace on.
🌐
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 »
🌐
BitDegree
bitdegree.org › learn › php-str-replace
Guide on PHP Str_Replace: Learn to Use Str_Replace Function
August 14, 2017 - By using the str_replace PHP function, you will replace a string specified in the first argument with a string specified in the second argument. The third argument is a string or an array in which search and replacement will take place.