Description Edit

str_replace returns an string or an array of an string with all occurances of the search value(s) replaced with the replace value(s).

Note: From version 4.0.5 of php onwards, all parameters can be arrays. However, due to a bug involved with arrays for both search and replace parameters, it is advised that arrays only be used in version 4.33 or later.

Parameters Edit

{{{returns}}} str replace (mixed search, mixed replace, mixed subject [, int count])

Parameter Infos Edit

Parameter PHP Version Type
str_replace 4 or 5 returns string or array
search 4 or 5 string or array (PHP >4.0.5)
replace 4 or 5 string or array (PHP >4.0.5)
subject 4 or 5 string or array (PHP >4.0.5)
count 5 integer

Examples Edit

Example 1 (Simple): Edit

 // Replace spaces with underscores
 $bodytag = str_replace(" ", "_", "Str replace");

Note where Str replace is, you can use a variable there aswell.

Output Edit


Example 2 (The use of arrays): Edit

 $phrase  = "I love classic music and to listen to the strings at the opera.";
 $search  = array("classic", "strings", "opera");
 $replace = array("techno",  "beats",   "disco");
 echo str_replace($search, $replace, $phrase);

Output Edit

I love techno music and to listen to the beats at the disco.

See also Edit

str_ireplace, substr_replace, preg_replace, and strtr

For more details on this function, visit its entry in the php manual.