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.
str_ireplace(find,replace,string,count)
Parameter | Is Required | Description |
---|---|---|
find | Required. | Value to find |
replace | Required. | Value to replace the value in find |
string | Required. | String to be searched |
count | Optional. | 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 java2s.com!" with "PHP":
<?php
echo str_ireplace("WORLD","PHP","Hello world from java2s.com!");
?>
The code above generates the following result.
Using str_replace() with an array and a count variable:
<?php
$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.
Using str_replace() with less elements in replace than find:
<?php
$find = array("HELLO","WORLD"); // This function is case-insensitive
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_ireplace($find,$replace,$arr));
?>
The code above generates the following result.