81 of 410 menu

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

str_ireplace(search, replace, subject);

Example

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

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

Code execution result:

'! ! !'

Example

Let's replace multiple variants in an array:

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

Code execution result:

'12c'

Example

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

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

Code execution result:

'!!!!!!'

Example

Let's perform a replacement in each array element:

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

Code execution result:

['!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
byenru