Replace the character "WORLD" (case-insensitive) in the string "Hello world!" with "Peter":
<?phpecho str_ireplace("WORLD","Peter","Hello world!");?>The str_ireplace() function replaces some characters in a string (case-insensitive).
The function must follow the following rules:
If the searched string is an array, then it will return an array.
If the string being searched is an array, then it will find and replace each element in the array.
If an array needs to be searched and replaced at the same time, and the elements to be replaced are less than the number of elements found, the excess elements will be replaced with empty strings.
If you search an array and replace only one string, the replacement string will apply to all found values.
Note: This function is not case sensitive. Please use the str_replace() function to perform a case-sensitive search.
Note: This function is binary safe.
str_ireplace( find,replace,string,count )
parameter | describe |
---|---|
find | Required. Specifies the value to be found. |
replace | Required. Specifies the value to replace the value in find . |
string | Required. Specifies the string to be searched for. |
count | Optional. A variable counting the number of substitutions. |
Return value: | Returns a string or array with replacement values. |
---|---|
PHP version: | 5+ |
Update log: | In PHP 5.0, the count parameter is added. |
Use str_ireplace() function with array and count variable:
<?php$arr = array("blue","red","green","yellow");print_r(str_ireplace("RED","pink",$arr,$i)); // This function is case-insensitiveecho "Replacements: $i";?>Use the str_ireplace() function with fewer elements to replace than found:
<?php$find = array("HELLO","WORLD"); // This function is case-insensitive$replace = array("B");$arr = array("Hello","world","! ");print_r(str_ireplace($find,$replace,$arr));?>