PHP Tutorial - PHP str_ireplace() Function






Definition

The str_ireplace() function replaces some characters with some other characters in a string.

This function is case-insensitive. Use the str_replace() function to perform a case-sensitive search.

Syntax

PHP str_ireplace() Function has the following syntax.

str_ireplace(find,replace,string,count)

Parameter

ParameterIs RequiredDescription
findRequired.Value to find
replaceRequired.Value to replace the value in find
stringRequired.String to be searched
countOptional.A variable that counts the number of replacements

If search and replace are arrays, then str_ireplace() takes a value from each array and uses them to search and replace on subject.

If replace has fewer values than search, then an empty string is used for the rest of replacement values.





Return

Returns a string or an array with the replaced values

Example

Replace the characters "WORLD" (case-insensitive) in the string "Hello world from java2s.com!" with "PHP":


<?php
echo str_ireplace("WORLD","PHP","Hello world from java2s.com!");
?>

The code above generates the following result.

Example 2

Using str_replace() with an array and a count variable:


<?php
$arr = array("blue","red","green","yellow");
print_r(str_ireplace("RED","pink",$arr,$i)); // This function is case-insensitive
echo "Replacements: $i";
?>

The code above generates the following result.





Example 3

Using str_replace() with less elements in replace than find:


<?php
$find = array("HELLO","WORLD"); // This function is case-insensitive
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_ireplace($find,$replace,$arr));
?>

The code above generates the following result.