Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท String Functions

PHP str_ireplace() function

Reference for PHP str_ireplace(): replace every occurrence of a search string ignoring letter case, with arrays, the count argument and case caveats.

str_ireplace() works exactly like str_replace(), except that the search ignores letter case. Every occurrence of the search text is replaced, whether it appears as cat, Cat or CAT.

The replacement text is inserted as you wrote it. PHP does not try to copy the capitalization of the match, so replacing hello with bye turns both Hello and HELLO into bye.

PHP
<?php
echo str_ireplace("php", "PHP", "Php and pHP");  // PHP and PHP

Try it

PHP Ignoring case

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Arrays and normalizing text

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
str_ireplace(
    array|string $search,
    array|string $replace,
    string|array $subject,
    int &$count = null
): string|array
Name Description
$search The value(s) to look for, matched without regard to case.
$replace The replacement(s). A single string is used for every search value; an array pairs by position and missing entries become empty strings.
$subject The string or array of strings to modify.
$count Optional by-reference variable that receives the number of replacements.

Return value: a string, or an array if $subject is an array.

Errors: a TypeError is thrown when $search is a string but $replace is an array.

Usage notes

  • Case folding is ASCII-oriented. Do not expect ร‰ and รฉ to match; for non-ASCII text use preg_replace() with the iu modifiers.
  • Array behavior is the same as str_replace(): searches are applied one after another, each on the result of the previous one. Use strtr() for simultaneous replacement.
  • The replacement keeps your capitalization, not the original's. Use preg_replace_callback() if you need to preserve case.
  • Slightly slower than str_replace(). Use the case-sensitive version when you know the case.
  • Not a security filter. Stripping words such as script this way is easy to bypass.
  • Empty search strings are ignored.