PHP str_ireplace() Function
Example
Replace the characters "WORLD" (case-insensitive) in the string "Hello world!" with "Peter":
Run Example »
Definition and Usage
The str_ireplace() function replaces some characters in a string (case-insensitive).
This function must follow these rules:
- If the search string is an array, then an array is returned.
- If the search string is an array, then it searches for and replaces each element in the array.
- If you need to search and replace in an array at the same time, and the number of elements to replace is less than the number of elements found, the extra elements are replaced with an empty string.
- If you search an array but replace with only a single string, the replacement string applies to all found values.
**Note:** This function is case-insensitive. Use the str_replace() function to perform a case-sensitive search.
**Note:** This function is binary-safe.
Syntax
str_ireplace(
find,replace,string,count
)
| Parameters | Description |
|---|---|
| find | Required. Specifies the value to find. |
| replace | Required. Specifies the replacement find value. |
| string | Required. Specifies the string to be searched. |
| count | Optional. A variable that counts the number of replacements. |
Technical Details
| Return Value: | Returns the string or array with replacement values. |
|---|---|
| PHP Version: | 5+ |
| Changelog: | In PHP 5.0, the count parameter was added. |
More examples
Example 1
Use the str_ireplace() function with arrays and a count variable:
Run Example »
Example 2
Use the str_ireplace() function with fewer elements to replace than elements found:
Run Example »
PHP String Reference Manual