PHP | str_ireplace() Function (original) (raw)

Last Updated : 11 Jul, 2025

The str_ireplace() is a built-in function in PHP and is used to replace all the occurrences of the search string or array of search strings by replacement string or array of replacement strings in the given string or array respectively. This function performs the search in a case-insensitive manner. This function is similar to str_replace() function. The difference is that the str_replace() function is case-sensitive whereas str_ireplace() is not.Syntax:

str_ireplace ( searchVal,searchVal, searchVal,replaceVal, subjectVal,subjectVal, subjectVal,count )

Parameters: This function accepts four parameters out of which 3 are mandatory and 1 is optional. All of these parameters are described below:

  1. $searchVal: This parameter can be of both string and array types. This parameter specifies the string to be searched and replaced.
  2. $replaceVal: This parameter can be of both string and array types. This parameter specifies the string with which we want to replace the $searchVal string.
  3. $subjectVal: This parameter can be of both string and array types. This parameter specifies the string or array of strings which we want to search for searchValandreplacewithsearchVal and replace with searchValandreplacewithreplaceVal.
  4. $count: This parameter is optional and if passed, it's value will be set to the total number of replacement operations performed on the string $subjectVal.

If the $searchVal and the $replaceVal arguments are arrays, then all the elements of the searchValargumentaresearchedinthesearchVal argument are searched in the searchValargumentaresearchedinthesubjectVal string and replaced by the corresponding elements in the replaceValargument.IfnumberofelementsinreplaceVal argument. If number of elements in replaceValargument.IfnumberofelementsinreplaceVal is less than that in searchValarray,thenifthereareanyoccurrencesoftheadditionalelementsofsearchVal array, then if there are any occurrences of the additional elements of searchValarray,thenifthereareanyoccurrencesoftheadditionalelementsofsearchVal argument in the subjectValargumentthentheywillbereplacedbyanemptystring.IfthesubjectVal argument then they will be replaced by an empty string. If the subjectValargumentthentheywillbereplacedbyanemptystring.IfthesubjectVal parameter is also an array instead of string then all of the elements of subjectValwillbesearched.∗∗ReturnValue∗∗:ThisfunctionreturnsastringoranarraybasedonthesubjectVal will be searched.Return Value: This function returns a string or an array based on the subjectValwillbesearched.ReturnValue:ThisfunctionreturnsastringoranarraybasedonthesubjectVal parameter with replaced values. Examples:

Input : subjectVal="HowAREyou",subjectVal = "How ARE you", subjectVal="HowAREyou",searcVal = "are" $replaceVal = "is" str_ireplace($searchVal,$replaceVal,$subjectVal); Output : How is you

Input : subjectVal="GeeksareGeeks",subjectVal = "Geeks are Geeks", subjectVal="GeeksareGeeks",searcVal = "are" $replaceVal = "for" str_ireplace($searchVal,$replaceVal,$subjectVal); Output : Geeks for Geeks

Below programs illustrate the str_ireplace() function in PHP:Program 1: This program shows that the str_ireplace() function is case-insensitive.

PHP `

res=strireplace("are","is",res = str_ireplace("are", "is", res=strireplace("are","is",subjectVal); echo $res; ?>

`

Output:

how is you

Program 2:

PHP `

res=strireplace("are","for",res = str_ireplace("are", "for", res=strireplace("are","for",subjectVal); echo $res; ?>

`

Output:

Geeks for Geeks

Reference: https://www.php.net/manual/en/function.str-ireplace.php