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
If subject is an array, then the search and replace is performed with every entry of subject, and the return value is an array as well. ... If passed, this will be set to the number of replacements performed.
🌐
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
I'm having some troubles with the PHP function str_replace when using arrays. I have this message: $message = strtolower("L rzzo rwldd ty esp mtdsza'd szdepw ty esp opgtw'd dple"); And I am tryin... 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() with associative array - Stack Overflow
E.g., ['ab' => 'c', 'c' => 'd'] ... with str_replace it will become 'd'. 2010-03-08T04:54:15.707Z+00:00 ... another thing is strtr is quite fast because the PHP directly uses the C version which is believed to be implemented using look up tables 2017-01-19T08:03:46.323Z+00:00 ... Save this answer. ... Show activity on this post. Copy$array_from_to = ... More on stackoverflow.com
🌐 stackoverflow.com
PHP str_replace array - Stack Overflow
i had a problem about returning an array into a spesified string. More on stackoverflow.com
🌐 stackoverflow.com
🌐
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
1 month ago - 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....
🌐
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 »
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php str_replace
PHP str_replace - PHP Tutorial
April 7, 2025 - ... The $count returns the number of replacements that the function performed. The $count is optional. If the $search and $replace arguments are arrays, the str_replace() takes each value from the $search array (from left to right) and replaces it with each value from the $replace array.
🌐
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.
Find elsewhere
🌐
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 = ...
🌐
Lsu
ld2011.scusa.lsu.edu › php › 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 = ...
🌐
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 - This will also make the str_replace function return an array. $count – The final parameter is entirely optional and allows you to retrieve how many replacements were performed To get the count, you need to pass in a variable. PHP will set the number of replacements to this variable.
🌐
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 - The search and replace parameters can either be strings or arrays. If both are arrays, the string in index 0 in the first array will be replaced with the string in index 0 in the second array, index 1 with index 1 and so on.
🌐
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.
🌐
Reintech
reintech.ai › blog › comprehensive-guide-php-str-replace-function
A Comprehensive Guide to PHP's `str_replace()` Function
April 14, 2023 - The function returns the modified string (or array of strings) with all replacements applied.