English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and Examples of PHP str_ireplace() Function

PHP String String Functions Manual

The str_ireplace() function is used to replace certain characters in a string with specified characters (case insensitive).

Syntax

str_ireplace(find,replace,string,count)

Definition and Usage

This function returns a string or array. The string or array is the result of replacing all find in string with replace (case-insensitive). If there are no special replacement rules, you should use this function to replace the preg_replace() function with i modifier.

If find and replace are arrays, then str_ireplace() will do a mapping replacement for both on subject. If the number of values in replace is less than the number of find, the extra replacements will be done with an empty string. If find is an array and replace is a string, then each element's replacement in find will always use this string.
Note: If find or replace is an array, their elements will be processed from the beginning to the end.

Return Value

 Returns the replaced string or array.

Parameter

Serial NumberParameters and Description
1

find

Required. The value to find

2

replace

Required. The replacement value for find. An array can be used to specify multiple replacements

3

string

Required. The string or array to be searched and replaced.
If string is an array, the replacement operation will traverse the entire string and also return an array.

4

count

Optional. If specified, it will count the replacements.

Online Example

Try the following example to replace elements in an array in a case-insensitive manner and return the count of replacements:

<?php
   //Replace elements in an array in a case-insensitive manner and return the count of replacements.
   $input = array("w3codeboxs",".com","simply","easy","learning");
   print_r(str_ireplace("w3codeboxs","w3codebox,$input,$i)); //Case insensitive
   
   echo "<br>" . "Replacement count: $i";  
?>
Test and see‹/›

Output result

Array
(
    [0] => w3codebox
    [1] => .com
    [2] => simply
    [3] => easy
    [4] => learning
)
Replacement count: 1

PHP String String Functions Manual