PHP Function Reference

PHP str_ireplace() Function



The PHP str_ireplace() function is a case-insensitive version of str_replace() function. It returns a string or an array with all occurrences of search in subject replaced with the given replace value.

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. If search is an array and replace is a string, then this replacement string is used for every value of search.

Note: This function replaces left to right, it might replace a previously inserted value when doing multiple replacements.

Syntax

str_ireplace(search, replace, subject, count)

Parameters

search Required. Specify value being searched for. An array may be used to designate multiple values to be searched for.
replace Required. Specify replacement value that replaces found search values. An array can be used to designate multiple replacements.
subject Required. Specify string or array being searched and replaced on. If it is an array, then the search and replace is performed with every element of the array, and return value is an array.
count Optional. If passed, this will be set to the number of replacements performed.

Return Value

Returns a string or an array with the replaced values.

Example:

In the example below, every occurrence of "Hello" in str is replaced by "Hi" using str_ireplace() function.

<?php
$str = "Hello World, HELLO PHP, hello Programming.";

$replaced_str = str_ireplace("Hello", "Hi", $str);
echo $replaced_str."\n";

//i is used to find the count of replacement
$replaced_str = str_ireplace("Hello", "Hi", $str, $i);
echo "Count of replacement: ".$i;   
?>

The output of the above code will be:

Hi World, Hi PHP, Hi Programming.
Count of replacement: 3

Example:

In the example below, every occurrence of "Hello" in Arr is replaced by "Hi" using str_ireplace() function.

<?php
$Arr = array("Hello", "World", "HELLO", "PHP", "hello", "Programming");

$replaced_Arr = str_ireplace("Hello", "Hi", $Arr);
print_r ($replaced_Arr);

//i is used to find the count of replacement
$replaced_Arr = str_ireplace("Hello", "Hi", $Arr, $i);
echo "\nCount of replacement: ".$i;  
?>

The output of the above code will be:

Array
(
    [0] => Hi
    [1] => World
    [2] => Hi
    [3] => PHP
    [4] => Hi
    [5] => Programming
)

Count of replacement: 3

Example:

Consider one more example, where the search and replace parameters are provided as arrays. Every element of search array present in the given string will be replaced by the respective element of replace array. Please note that this function is case-insensitive.

<?php
$str = "The quick brown fox jumps over the lazy dog.";

$search = ['FOX', 'Dog', 'BrowN'];
$replace = ['wolf', 'cat', 'black'];

$result = str_ireplace($search, $replace, $str);

echo $result; 
?>

The output of the above code will be:

The quick black wolf jumps over the lazy cat.

❮ PHP String Reference