str_ireplace
Replaces all case-insensitive occurrences of a search string with a replacement string.
str_ireplace(array|string $search, array|string $replace, string|array $subject, int &$count = null): string|array- Added in PHP 5.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$search | array|string | The value or values to search for. An array designates multiple needles. |
$replace | array|string | The replacement value or values. If search is an array and replace is a string, every needle is replaced by that string. |
$subject | string|array | The string or array of strings to search in. An array subject yields an array result. |
$countoptional | int= null | If supplied, set by reference to the number of replacements performed. |
Return value
string|arrayThe subject with all case-insensitive matches replaced. Returns an array when subject is an array, otherwise a string.
Errors
No exceptions are documented. Arrays of different lengths are not an error: if search is an array longer than replace, the surplus needles are replaced with an empty string.
Changelog
- PHP 8.2Case folding no longer depends on setlocale(). Only ASCII case folding is performed; non-ASCII bytes are compared by value.
str_ireplace is str_replace with case-insensitive matching. The needle is matched regardless of case, but the replacement is inserted exactly as you wrote it — the original casing is not preserved.
When to reach for it
Reach for it when the input casing is out of your control: user-typed keywords, imported CSV values, legacy database content, email subjects. Typical jobs are censoring a word list, normalising vendor spellings, or swapping placeholder tokens that might be written {NAME} or {name}.
What trips people up
Replacement runs left to right and the result of one pass is visible to the next, so replacing ['a', 'b'] with ['b', 'c'] turns every a into c. When your replacements can feed each other, use strtr with an associative array — it replaces in a single pass and never revisits inserted text.
Case folding is ASCII-only since PHP 8.2, so accented letters are matched by byte value: É will not match é. For Unicode-aware case-insensitive replacement you need preg_replace with the iu modifiers or the intl extension.
If search is an array longer than replace, the surplus needles are replaced with an empty string.
Example
When to use it
- Censor a list of banned words no matter how users capitalise them
- Normalise inconsistent vendor or brand spellings in imported data
- Fill template placeholders that may appear as {name} or {NAME}
- Strip a keyword out of user-supplied search text before querying
Frequently asked questions
What is the difference between str_replace and str_ireplace?
str_replace matches the needle exactly, str_ireplace ignores case when matching. Both insert the replacement verbatim, and str_ireplace is slightly slower because of the case folding.
Does str_ireplace preserve the original capitalisation?
No. It finds the match regardless of case but writes the replacement exactly as you supplied it, so 'Php' replaced with 'PHP' becomes 'PHP'. Use preg_replace_callback if you need to rebuild the original casing.
Does str_ireplace work with accented characters?
Only for ASCII. Since PHP 8.2 case folding is ASCII-only and non-ASCII bytes are compared by value, so É does not match é. Use preg_replace with the iu modifiers for Unicode-aware matching.
More examples
Multiple needles and the chaining trap
The first line prints "C C": A became B, and the second pass then turned that new B into C. strtr on the second line replaces in one pass and correctly prints "B C". The third line shows the other array form — several needles collapsing to a single string replacement.
<?php
$s = 'A B';
echo str_ireplace(['a', 'b'], ['b', 'c'], $s), "\n";
echo strtr($s, ['A' => 'B', 'B' => 'C']), "\n";
echo str_ireplace(['red', 'blue', 'green'], 'COLOUR', 'Red, BLUE and green'), "\n";Counting matches and handling leftovers
Three needles are given but only two replacements, so "billing" would be replaced with an empty string if it occurred. The count reflects every match across all needles, including the two casings of "sales", which is how you check whether anything was actually rewritten.
<?php
$body = 'Contact SALES or [email protected], not Support.';
str_ireplace(['sales', 'support', 'billing'], ['[S]', '[H]'], $body, $n);
echo str_ireplace(['sales', 'support', 'billing'], ['[S]', '[H]'], $body), "\n";
echo "count: {$n}\n";
Discussion