PHP,错误 preg_match_all 错误未知修饰符?

发布于 2024-12-05 04:17:01 字数 638 浏览 0 评论 0原文

我收到以下错误

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

じее 2024-12-12 04:17:01

$k 或 $m 可能包含 /w 。你必须逃离他们

$m = str_replace('/', '\\/', $m);
$k = str_replace('/', '\\/', $k);

$k or $m includes /w probably. You have to escape them

$m = str_replace('/', '\\/', $m);
$k = str_replace('/', '\\/', $k);
森林散布 2024-12-12 04:17:01

您可以通过插入当时恰好存在的任何 $k 来创建任意正则表达式字符串。如果 $k 包含任何正则表达式元字符,您最终会遇到相当于 sql 注入攻击的正则表达式。您需要使用 preg_quote() 来清理 $k:

preg_match_all("/" . preg_quote($k) . "/i", $text1, $matches);'

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:

preg_match_all("/" . preg_quote($k) . "/i", $text1, $matches);'
小糖芽 2024-12-12 04:17:01

其中一个关键字包含斜杠。

这会导致您的正则表达式提前终止(在该斜杠处),并且以下字符(在本例中为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文