PHP str_ireplace Function is an inbuilt function in PHP. It replaces all the occurrences of a string or an array of string in another string or an array of string respectively in a case-insensitive manner. In this article, we will discuss the PHP str_ireplace Function. Also, we will discuss a few examples of using it.
This function is similar to the PHP str_replace Function except str_replace is case-sensitive while str_ireplace is not.
str_ireplace ( $search, $replace, $subject, $count )
The PHP str_ireplace Function expects four parameters. However, only three parameters are mandatory and one is optional. The description of the parameters is as follows:
- $search: The search parameter specifies the string to search. Also, it can be an array to specify multiple search strings.
- $replace: The replace parameter specifies the string to replace with. Also, it can be an array to specify multiple replacements.
- $subject: The subject string is the string or an array of string which will be searched and replaced on.
- $count: The count parameter, if passed, is set to the total number of replacements done by the function. It is an optional parameter.
Points to Remember
Always remember the following points while working with str_ireplace function in PHP.
- If search and replace are arrays, then the function takes a value from each array and uses them to search and replace on the subject.
- Also, If replace has lesser values than search string, then the function assumes blank strings for the remaining values.
- If the search is an array and replace is a string, then the function searches for all values from search array and replaces the single string. However, the opposite is not true.
- If the subject is an array, then the function performs search and replace on every element of the array. Also, the return type of the function is also an array in this case.
The PHP str_ireplace Function returns a string or an array of strings after replacing values. Also, it searches the string in a case-insensitive manner.
<?php $search = 'world'; $replace = 'There'; $subject = 'Hello WORLD!'; $finalString = str_ireplace($search, $replace, $subject); echo $finalString; ?>
In the above example, the str_replace function in PHP replaces all occurrences of ‘world’ with ‘There’ in the subject string. Also, do notice that the function performs the replace operation in case-insensitive manner.
OUTPUT: Hello There!
For more detailed examples, you can refer to PHP str_replace Function.
Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.