PHP preg_replace: Case insensitive match with case sensitive replacement

后端 未结 3 1954
囚心锁ツ
囚心锁ツ 2020-12-14 07:39

I\'m using preg_replace in PHP to find and replace specific words in a string, like this:

$subject = \"Apple apple\";
print preg_replace(\'/\\bapple\\b/i\',          


        
3条回答
  •  旧时难觅i
    2020-12-14 08:17

    This is the solution that I used:

    $result = preg_replace("/\b(foo)\b/i", "$1", $original);
    

    In the best words that I can I'll try explain why this works: Wrapping your search term with () means I want to access this value later. As it is the first item in pars in the RegEx, it is accessible with $1, as you can see in the substitution parameter

提交回复
热议问题