W3docs

str_ireplace()

Our article is about the PHP function str_ireplace(), which is used to replace a string in a given string with another string, ignoring case sensitivity. This

The str_ireplace() function in PHP replaces all occurrences of a search string with a replacement string, ignoring case. It is the case-insensitive twin of str_replace(): everything works the same way, except the search is not sensitive to upper- and lower-case letters. This makes str_ireplace() the right choice when the casing of the target text is unknown or inconsistent — for example, when cleaning up user input or normalizing tags that may appear as PHP, php, or Php.

This article covers the syntax of str_ireplace(), how each parameter behaves, how to work with arrays, how to count replacements, and the gotchas to watch out for.

Syntax

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

The function takes three required parameters and one optional one:

  • $search — the value (or array of values) to look for.
  • $replace — the value (or array of values) to replace it with.
  • $subject — the string (or array of strings) to search inside.
  • $count (optional) — a variable passed by reference that is filled with the total number of replacements performed.

It returns the modified $subject. If $subject is an array, an array is returned with the replacement applied to every element.

Basic example

php— editable, runs on the server

Even though we search for "FOX" in uppercase, str_ireplace() ignores case and matches the lowercase "fox" in the subject. The output is:

The quick brown cat jumps over the lazy dog

With the standard, case-sensitive str_replace(), the same call would find no match and the string would be returned unchanged.

Using arrays for search and replace

When $search and $replace are both arrays, each search value is paired with the replacement at the same index. This lets you perform several substitutions in one call:

<?php
$search  = ["red", "GREEN", "Blue"];
$replace = ["crimson", "emerald", "navy"];
$subject = "Red, green and blue are colors.";

echo str_ireplace($search, $replace, $subject);
// crimson, emerald and navy are colors.
?>

Notice that the casing of the entries in $search does not matter — "GREEN" still matches "green". If $replace has fewer elements than $search, the missing replacements are treated as empty strings (the matches are removed).

When $search is an array but $replace is a single string, that one string replaces every search value:

<?php
echo str_ireplace(["cats", "dogs"], "pets", "I have CATS and Dogs.");
// I have pets and pets.
?>

Counting replacements with $count

Pass a fourth argument by reference to learn how many replacements were made:

<?php
$text   = "PHP is great. php is fun. PhP rocks.";
$result = str_ireplace("php", "PHP", $text, $count);

echo $result . "\n";       // PHP is great. PHP is fun. PHP rocks.
echo "Replacements: $count"; // Replacements: 3
?>

The three variants PHP, php, and PhP are all matched, so $count is 3. This is handy for validation — for example, checking whether a forbidden word appeared at all.

Gotchas to keep in mind

  • The replacement is not searched again. str_ireplace() processes search terms left to right and never re-scans text it has already inserted, so it cannot loop infinitely. But order still matters when one search term is a substring of another.
  • It operates on bytes, not multibyte characters. For UTF-8 text where case-insensitivity must respect accented or non-Latin letters, str_ireplace() may not behave as expected. Use PHP's mb_* string functions or preg_replace() with the i modifier instead.
  • Use str_replace() when case matters. If you need exact, case-sensitive matching, reach for str_replace() — it is slightly faster because it skips the case-folding step.

Summary

str_ireplace() performs case-insensitive search-and-replace on strings. Use it for simple, literal substitutions where casing should be ignored; use str_replace() when case matters, and preg_replace() when you need pattern-based or multibyte-aware matching.

Practice

Practice
What does the str_ireplace() function in PHP do?
What does the str_ireplace() function in PHP do?
Was this page helpful?