Home >>PHP String Functions >PHP str_replace() Function
PHP str_replace() function is used to replace some characters of a given string with other characters. It replaces all the characters of the given string which matches with the replacement string. It is case-sensitive & binary-safe.
Syntax:
str_replace( $find, $replace, $string, $count );
Parameter | Description |
---|---|
find | This is a required parameter. This parameter defines the value to find. |
replace | This is a required parameter. This parameter defines the value to replace the value in find. |
string | This is a required parameter. This parameter contains the string to be searched. |
count | This is an optional parameter. This parameter contains a variable that counts the number of replacements. |
Here is an example of str_replace() function in PHP:
<html> <body> <?php $str = "Hello everyone.. I am Abhi.."; echo str_replace("Abhi","Jerry",$str); ?> </body> </html>
Here is an another example of str_replace() function in PHP:
<html> <body> <?php $str= "Red yellow pink green red black red blue black white red black"; echo str_replace("red","Black",$str,$i); // Case-sensitive echo "<br>" . "Replacements: $i"; ?> </body> </html>