我想匹配字符串中的文本,而忽略它的字母大小写。如果字母不一致,我的例子就会失败。php:如何忽略preg_replace内的字母大小写?
/*
$text: fOoBaR
$str: little brown Foobar
return: little brown Foobar
*/
function bold($text, $str) {
// This fails to match if the letter cases are not the same
// Note: I do not want to change the returned $str letter case.
return preg_replace('/('.str_replace(array("/", "\\"), array("\/", "\\"), $text).')/', "$1", $str);
}
$phrase = [
'little brown Foobar' => 'fOoBaR',
'slash/slash' => 'h/sl',
'spoon' => 'oO',
'capital' => 'CAPITAL',
];
foreach($phrase as $str => $text)
echo bold($text, $str) . '
';
2014-11-22
ditto
+1
符合正则表达式的字符串的整数部分添加修改'i'的正则表达式。 '')/''将会是'')/ i''。另外,使用regexp中的'$ text'代替'preg_quote'函数的结果更安全。所以..'preg_replace('/'。preg_quote($ text)。'/ i','$ 0',$ str);' –
2014-11-22 00:23:39
+0
辉煌,感谢的家伙。你昨天也帮助我。 :D你怎么不写你的答案作为答案寿? –
2014-11-22 00:25:01
+0
我不喜欢写太多的文字或者很多解释。特别是当答案是单线时。 –
2014-11-22 00:26:49