Backend Development 6 min read

Using PHP str_ireplace() Function for Case-Insensitive String Replacement

This article explains PHP's str_ireplace() function, detailing its parameters, usage differences from str_replace(), and provides multiple code examples demonstrating case-insensitive replacements in strings, arrays, and combined search/replace scenarios, along with a brief summary of its capabilities.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP str_ireplace() Function for Case-Insensitive String Replacement

PHP's str_ireplace() function is used to replace specified content within a string without case sensitivity, similar to str_replace() but case‑insensitive.

str_ireplace() function basic usage

<code>str_ireplace($search, $replace, $subject, $count);</code>

Where $search is the string or array to be replaced, $replace is the replacement string or array, $subject is the target string or array, and $count (optional) stores the number of replacements performed.

Parameter purpose and values

$search: content to be replaced

$search can be a string or an array of strings. If it is an array, each element is paired with the corresponding element in $replace for replacement. If it is a single string, all occurrences of that string are replaced.

$replace: replacement content

$replace can also be a string or an array. When $search is an array, the elements of $replace are matched one‑to‑one with $search . If $search is a single string, every matched element is replaced by the value of $replace .

$subject: target string or array

$subject may be a string or an array of strings. If it is an array, each element is processed with the corresponding $search and $replace elements. If it is a single string, all matches of $search within it are replaced.

$count: optional variable to store the number of replacements

$count is an integer that receives the total number of replacements performed. If omitted, all matches are replaced without counting.

str_ireplace() usage examples

Example 1: Replace a single word in a string

<code>$string = "Hello World";
$new_string = str_ireplace("world", "PHP", $string);
echo $new_string; // Output: Hello PHP</code>

This replaces the word "world" with "PHP" regardless of case.

Example 2: Replace multiple words in a string

<code>$string = "Hello World";
$search = array("hello", "world");
$replace = array("PHP", "World");
$new_string = str_ireplace($search, $replace, $string);
echo $new_string; // Output: PHP World</code>

Here the arrays $search and $replace are paired element‑by‑element, converting "hello" to "PHP" and "world" to "World".

Example 3: Replace words in an array of strings

<code>$strings = array("Hello World", "Hello PHP");
$search = array("hello", "world");
$replace = array("PHP", "World");
$new_strings = str_ireplace($search, $replace, $strings);
print_r($new_strings); // Output: Array ( [0] => PHP World [1] => PHP PHP )</code>

Both strings in the array are processed, with each matching element replaced according to the paired arrays.

Summary

The str_ireplace() function in PHP provides case‑insensitive string replacement, mirroring the behavior of str_replace() but ignoring letter case. It can replace single characters, whole words, or multiple items in strings and arrays, making it a versatile tool for text manipulation.

PHPTutorialstring replacementstr_ireplace
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.