PHP function str_ireplace() – Case-insensitive version of str_replace()

The str_ireplace() function is used to replace some case insensitive characters in a string. This function was introduced in PHP5. Performs a case-insensitive search for all occurrences of search in string and replaces them with replace. If all three parameters are strings, a string is returned. If string is an array, the replacement is performed for every element in the array and an array of results is returned. If search and replace are both arrays, elements in search are replaced with the elements in replace with the same numeric indices. Finally, if search is an array and replace is a string, any occurrence of any element in search is changed to replace. If supplied, count is filled with the number of instances replaced.

Syntax:

str_ireplace(
    array|string $search,
    array|string $replace,
    string|array $subject,
    int &$count = null
): string|array

Parameters

Parameter Description
search The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
replace The replacement value that replaces found search values. An array may be used to designate multiple replacements.
subject The string or array being searched and replaced on, otherwise known as the haystack.
count If passed, this will be set to the number of replacements performed.

Return Values

Returns a string or an array of replacements.

Example

Example 1:

<?php
$bdtag = str_ireplace(“%bd%”, “blue”, “<body text=%BD%>”);
?>

Example 2:

$string = "Adam went home";
$newstring = str_ireplace("adam", "John", $string);
print $newstring;

With that code, $newstring will be printed out as “John went home”. So it ignores the “A” in “Adam” as capital letter.

Example 3:

<?php
$bodytag = str_ireplace("%body%", "black", "");
echo $bodytag; // 
?>
Related Post