phpstringreplacecase-insensitive

Make str_replace() case-insensitive


I have a sample code:

$text = "abc ABC def ghi Abc aBc xyz";
$search = "abc"
$string = str_replace(" " . trim($search) . " ", 'DEF', $text);
echo $string;

This code currently doesn't make any changes.

The desired result is: abc DEF def ghi DEF DEF xyz

How to fix it?


Solution

  • You can using:

    $regex = '/(\s)'.trim($search).'(\s)/i';
    preg_match_all($regex, $text, $tmp3)