![](/img/trans.png)
[英]Error: Nothing to repeat at offset error during a preg_match_all in PHP
[英]Error during a preg_match_all in PHP
我需要检查字符串是否包含特定单词。
我的代码如下:
// Get index.html source
$html = file_get_contents('extract/index.html');
// Bad words checker
$badWords = array("iframe", "alert");
$matches = array();
$matchFound = preg_match_all("/\b(" . implode($badWords,"|") . ")\b/i", $html, $matches);
if ($matchFound) {
$words = array_unique($matches[0]);
foreach($words as $word) {
$results[] = array('Error' => "Keyword found : ". $word);
}
}
else {
$results[] = array('Success' => "No keywords found.");
}
每次我要执行此操作时,都会出现以下警告:
Warning: preg_match_all(): Unknown modifier 'w' in /home/public_html/upload.php on line 131
第131行:
$matchFound = preg_match_all("/\b(" . implode($badWords,"|") . ")\b/i", $html, $matches);
你知道为什么吗 ?
谢谢。
如果其中一个不好的词是“ / w”,则可能导致此问题。 下面的示例演示了这一点:
$html = 'foobar';
// Bad words checker
$badWords = array("iframe", "alert", '/w');
$matches = array();
$matchFound = preg_match_all("/\b(" . implode($badWords,"|") . ")\b/i", $html, $matches);
'/ w'的变体(例如'foo / wbar'或'/ wfoo')也会导致此问题。 仔细阅读badWord并删除有问题的单词。
编辑:另一个解决方案是使用不同的定界符,如#。 像这样:
$matchFound = preg_match_all("#\b(" . implode($badWords,"|") . ")\b#i", $html, $matches);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.