The str_ireplace Function

The str_ireplace function searches a string for all occurrences of a substring (case-insensitive) and replaces them with a specified value. The first parameter is the substring to search for, the second is the replacement string, and the third is the string to search in.

Syntax

phpstr_ireplace(search, replace, subject);

Example

Let's replace all occurrences of a substring without case sensitivity:

php<?php
  echo str_ireplace('abc', '!', 'aBc abc ABC');
?>

Code execution result:

php'! ! !'

Example

Let's replace multiple variants in an array:

php<?php
  $res = str_ireplace(['a', 'b'], ['1', '2'], 'aBc');
  echo $res;
?>

Code execution result:

php'12c'

Example

Let's replace the specified letters with one common specified character:

php<?php
  echo str_ireplace(['a', 'b', 'c'], '!', 'AbcAbc');
?>

Code execution result:

php'!!!!!!'

Example

Let's perform a replacement in each array element:

php<?php
  $arr = ['Abc', 'abc', 'abc'];
  $res = str_ireplace('a', '!', $arr);
  print_r($res);
?>

Code execution result:

php['!bc', '!bc', '!bc']

See Also

  • the str_replace function,
    which performs case-sensitive replacement
  • the substr_replace function,
    which replaces a part of a string