PHP str_ireplace() Function
PHP str_ireplace() Function
The PHP str_ireplace() function in PHP replaces some characters with some other characters in a string.
Syntax
str_ireplace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
Parameter
search(required)- This parameter specifies the value being searched for.
replace(required)- This parameter represents the replacement value that replaces found search values.
subject(required)- This parameter signifies the string or array being searched and replaced.
count(optional)- It signifies a variable that counts the number of replacements.
Return
This function returns a string or an array of replacements.
Example 1
<?php //initializing the actual string $str= "Hello World!"; echo "The original string:".$str; // the value to replace the value in find $str1= "WORLD"; // the string to be searched $str2= "PHP"; // replaces some characters with some other characters in a string echo "\nAfter the str_ireplace() function...\n"; echo "New string: ".str_ireplace($str1,$str2,"Hello world!"); ?>
Output
The original string:Hello World! After the str_ireplace() function... New string: Hello PHP!
Example 2
<?php
//Case-insensitive
$find = array("Example","And","Tutorial");
$replace = array("T");
$arr = array("Example","and");
echo "After using the str_ireplace()...";
echo "\n";
print_r(str_ireplace($find,$replace,$arr));
?>
Output
After using the str_ireplace()... Array ( [0] => T [1] => )
Example 3
<?php
// initializing the string
$find = array("HELLO","WORLD");
$replace = array("D");
$arr = array("Hello","world","$");
print_r(str_ireplace($find,$replace,$arr));
?>
Output
Array ( [0] => D [1] => [2] => $ )
