PHP str_ireplace() function

Example

Replace the character "WORLD" (case-insensitive) in the string "Hello world!":

<?php
echo str_ireplace("WORLD","Shanghai","Hello world!");
?>

Run Instances

Definition and Usage

The str_ireplace() function replaces some characters in a string (case-insensitive).

The function must follow the following rules:

  • If the search string is an array, then it will return an array.
  • If the search string is an array, then it will perform search and replacement on each element of the array.
  • If both search and replacement in an array are required, and the number of elements to be replaced is less than the number of elements found, the extra elements will be replaced with an empty string.
  • If the search is for an array but only a string is to be replaced, then the replacement string will take effect on all found values.

Note:This function is not case-sensitive. Please use str_replace() Function to perform case-sensitive search.

Note:This function is binary safe.

Syntax

str_ireplace(find,replace,string,count)
Parameters Description
find Required. Specifies the value to be searched for.
replace Required. Specifies the replacement find of the value of the value.
string Required. Specifies the string to be searched.
count Optional. A variable to count the replacement numbers.

Technical Details

Return Value: Returns a string or array with the replacement values.
PHP Version: 5+
Update Log: In PHP 5.0, a new count Parameter.

More Examples

Example 1

Use with an array and count The str_ireplace() function of variables:

<?php
$arr = array("blue","red","green","yellow");
print_r(str_ireplace("RED","pink",$arr,$i)); // This function is case-insensitive
echo "Replacement count: $i";
?>

Run Instances

Example 2

Use the str_ireplace() function with fewer elements to be replaced than found:

<?php
$find = array("HELLO","WORLD");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_ireplace($find,$replace,$arr));
?>

Run Instances