Home > Article > Backend Development > str_replace() function in PHP
str_replace() function is used to replace one string with another string.
Note - This function is case sensitive.
str_replace(find, replace, str, count)
find - The value to search for
replace - The string we want to replace $find with
str - The string we want to search for
count - Number of replacements
str_replace() function returns a string or array with replacement values.
The following is an example-
Real-time demonstration
<?php $str = "I am Amit"; $res = str_replace("Amit", "David", $str); echo $res; ?>
I am David
The following is an example -
Live demonstration
<?php $myArr = array("one","two","three"); print_r(str_replace("three","four",$myArr,$c)); echo "<br>" . "Number of Replacements = $c"; ?>
Array ( [0] => one [1] => two [2] => four ) <br> Number of Replacements = 1
The above is the detailed content of str_replace() function in PHP. For more information, please follow other related articles on the PHP Chinese website!