Fandom

PHP Wiki

str_replace

51pages on
this wiki
Add New Page
Talk0 Share


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

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

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

That's 2 celver by half and 2x2 clever 4 me. Thanks! That's 2 celver by half and 2x2 clever 4 me. Thanks!

Example 2 (The use of arrays): Edit

<?php
 $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.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.