PHP,错误 preg_match_all 错误未知修饰符?
我收到以下错误
[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);
}
}
我真的很困惑问题是什么?
I'm getting the following error
[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
The value of search is "repair a 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);
}
}
I'm really quite puzzled what the problem is ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$k 或 $m 可能包含
/w
。你必须逃离他们$k or $m includes
/w
probably. You have to escape them您可以通过插入当时恰好存在的任何 $k 来创建任意正则表达式字符串。如果 $k 包含任何正则表达式元字符,您最终会遇到相当于 sql 注入攻击的正则表达式。您需要使用
preg_quote()
来清理 $k:You're creating arbitrary regex strings by inserting whatever $k happens to be at the time. If $k contains any regex metacharacters, you'll end up with the regex equivalent of sql injection attacks. You need to use
preg_quote()
to sanitize $k:其中一个关键字包含斜杠。
这会导致您的正则表达式提前终止(在该斜杠处),并且以下字符(在本例中为
w
)被解释为无效修饰符。解决方案:在将关键字添加到正则表达式之前,先对关键字调用
preg_quote()
。One of the keywords contains a slash.
This causes your regex to be terminated prematurely (at that slash) and the following character (in this case
w
) is interpreted as an invalid modifier.Solution: Call
preg_quote()
on your keywords before adding them into the regex.