2 Demos of PHP str_ireplace functions (case-insensitive replacement)

2 Demos of PHP str_ireplace functions (case-insensitive replacement)

The str_ireplace function

In order to replace words in your strings with other words, you can use replace string function str_ireplace() in PHP. You can

You can use str_replace() as well, however, this is a case sensitive function while str_ireplace() is not.

See an example of str_ireplace

PHP str_ireplace Syntax

Following is the syntax of using the str_ireplace function for case-insensitive words replacement:

str_ireplace(find,replace,string,count)

Where:

  1. Find (value to be replaced).
  2. Replace (value to be replaced by).
  3. String – that can be a string variable, static or an array.

An example of str_ireplace function

The following example uses the str_ireplace function to replace one word with another – both words use different cases for the demo.

Experience this example online

You can see, the name Mike is used with the first capital in the source string.

In the str_ireplace function, the small letter is used “mike” for demo purpose and it still found and replaced the name with Peter.

See another example with a variable

The outcome of this example is the same as in above example, except we used a variable in the str_ireplace function. See the code and output below or by clicking the link:

Experience this example online

Output

Hello Peter!

Also see PHP strings