The str_replace() is the built-in function in PHP and is used to replace all the characters in a string. This function is case-sensitive, if need to perform case-insensitive match and replace we can use str_ireplace() function.
The str_replace() function works by the following rules :
- If a string to be searched is that the array, it returns an array.
- If a string to be searched is that the array, find and replace is performed with an each array element.
- If both find and replace are arrays, and replace has fewer elements than find, the empty string are used as a replacement.
- If the finding value is an array, and replacement is that the string, the replacement string are used for each find value.
str_replace(find, replace, string, count)
find – The find parameter is required, and it specifies the value to find.
replace – The replace parameter is required, and it specifies the value to replace the value in find.
string – The string parameter is required, and it specifies the string to be searched.
Count – The count parameter is optional, and it is the variable that counts the number of replacements.
<?php $str = 'Welcome to HTML Tutorials'; // Display replaced string echo str_replace("HTML", "PHP", $str); echo '<br>'; echo str_ireplace("html", "PHP", $str); ?>