PHP Tutorial - PHP str_ireplace() Function


The str_ireplace() function replaces some characters with some other characters in a string.

This function is case-insensitive. Use the str_replace() function to perform a case-sensitive search.


PHP str_ireplace() Function has the following syntax.



ParameterIs RequiredDescription
findRequired.Value to find
replaceRequired.Value to replace the value in find
stringRequired.String to be searched
countOptional.A variable that counts the number of replacements

If search and replace are arrays, then str_ireplace() takes a value from each array and uses them to search and replace on subject.

If replace has fewer values than search, then an empty string is used for the rest of replacement values.


Returns a string or an array with the replaced values


Replace the characters "WORLD" (case-insensitive) in the string "Hello world from!" with "PHP":

echo str_ireplace("WORLD","PHP","Hello world from!");

The code above generates the following result.

Example 2

Using str_replace() with an array and a count variable:

$arr = array("blue","red","green","yellow");
print_r(str_ireplace("RED","pink",$arr,$i)); // This function is case-insensitive
echo "Replacements: $i";

The code above generates the following result.

Example 3

Using str_replace() with less elements in replace than find:

$find = array("HELLO","WORLD"); // This function is case-insensitive
$replace = array("B");
$arr = array("Hello","world","!");

The code above generates the following result.