PHP str_ireplace() Function
Definition
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.
Syntax
PHP str_ireplace() Function has the following syntax.
str_ireplace(find,replace,string,count)
Parameter
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.
Return
Returns a string or an array with the replaced values
Example
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.
Example 2
Using str_replace() with an array and a count variable:
<?php/* w ww .j av a 2 s . co m*/
$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:
<?php/* w ww . ja v a 2s.co m*/
$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.