PHP str_ireplace() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, str_ireplace() replaces every occurrence of a search value inside a string, ignoring upper and lower case.

It works like str_replace(), but "CAT", "Cat", and "cat" all match. You can also pass arrays, and read how many replacements were made.

Syntax

Call str_ireplace() with the values you need. Start with this line.

PHP
$new = str_ireplace("world", "PHP", $text);
CallWhat it does
str_ireplace("WORLD", "PHP", "Hello world")Returns "Hello PHP".
str_ireplace("a", "4", $text, $count)Replaces every a or A and stores the total in $count.
str_ireplace(["red", "blue"], ["green", "yellow"], $text)Applies several replacements in order.

If the subject is an array, str_ireplace() returns an array with the replacements applied to each element. For case-sensitive replacing, use str_replace() instead.

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

Parameters

ParameterDescription
searchRequired. The value (or array of values) to look for, ignoring case.
replaceRequired. The replacement text, or an array of replacements that matches search.
subjectRequired. The string or array of strings to search and replace in.
countOptional. A variable that receives the number of replacements made.

Return Value

Return ValueDescription
stringThe new string with replacements applied (when subject is a string).
arrayAn array of new strings (when subject is an array).

PHP Examples

Example 1 — Replace Text Ignoring Case

The search text is lowercase, but the string uses a different case. It still matches.

PHP
<?php
$text = "Hello WORLD, welcome to the World of code.";
$new = str_ireplace("world", "PHP", $text);

echo $new, PHP_EOL;

How It Works

  1. Set the text. It contains WORLD and World in different cases.
  2. Call str_ireplace. The search value is "world" in lowercase.
  3. Both matches are replaced. Case does not matter during matching.
  4. Print the result. The replacement "PHP" is inserted exactly as typed.

Example 2 — Count the Replacements

Pass a fourth variable to learn how many times a replacement happened.

PHP
<?php
$text = "Cat, cAt, CAT";
$new = str_ireplace("cat", "dog", $text, $count);

echo $new, PHP_EOL;
echo "Replacements: ", $count, PHP_EOL;

How It Works

  1. Create text with three spellings of the same word.
  2. Pass $count as the fourth argument. PHP fills it in for you.
  3. All three variants become "dog".
  4. Print $count to confirm that three replacements were made.

Example 3 — Use Arrays for Search and Replace

Pass two arrays to run several replacements in one call.

PHP
<?php
$search = ["red", "BLUE"];
$replace = ["green", "yellow"];

echo str_ireplace($search, $replace, "Red and blue"), PHP_EOL;

How It Works

  1. Build a search array and a replace array.
  2. Items are paired by position: red becomes green, BLUE becomes yellow.
  3. Case is ignored, so "Red" and "blue" still match.
  4. PHP returns one final string with both changes.

Frequently Asked Questions

str_ireplace() finds a search value in a string and replaces it with another value, ignoring upper and lower case. It returns the new string and leaves the original unchanged.
str_replace() is case-sensitive, so "Cat" does not match "cat". str_ireplace() ignores case, so both match. Everything else works the same.
Pass a variable as the fourth argument. PHP stores the total number of replacements in it, for example str_ireplace('a', 'b', $text, $count).
Yes. If the subject is an array, PHP applies the replacements to every element and returns a new array. The search and replace values can also be arrays.
No. The replacement text is inserted exactly as you typed it. If you need to match the original case, use preg_replace_callback() with your own logic.

Next: str_pad()

str_pad() pads a string to a given length by adding characters to the left, right, or both.

str_pad() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful