我收到以下错误
[13-Sep-2011 07:26:28] PHP Warning: preg_match_all() [<a
href='function.preg-match-all'>function.preg-match-all</a>]: Unknown
modifier 'w' in D:\domains\wwwroot\php\search.php on line 274
搜索的价值是“修复pst”
$text1 = $result['ProgramName'] . " " . $result['ProgramVersion'];
$keywords1 = explode(" ",stripslashes($search));
foreach ($keywords1 as $k){
preg_match_all("/$k/i",$text1,$matches);
foreach ($matches[0] as $m){
$text1 = preg_replace("/$m/", '<span class="highlight">'.$m.'</span>', $text1);
}
}
我真的很困惑这个问题是什么?
答案 0 :(得分:1)
$ k或$ m可能包括/w
。你必须逃脱它们
$m = str_replace('/', '\\/', $m);
$k = str_replace('/', '\\/', $k);
答案 1 :(得分:1)
你通过插入当时恰好是$ k来创建任意正则表达式字符串。如果$ k包含任何正则表达式元字符,那么你最终会得到相当于sql注入攻击的正则表达式。您需要使用preg_quote()
来清理$ k:
preg_match_all("/" . preg_quote($k) . "/i", $text1, $matches);'
答案 2 :(得分:0)