我有一个正常的表达式,通过HTML内容寻找一些曾经工作的关键字,但现在失败了,我不明白为什么。 (正则表达式来自this thread)查找和替换在HTML正则表达式失败
$find = '/(?![^<]+>)(?<!\w)(' . preg_quote($t['label']) . ')\b/s';
$text = preg_replace_callback($find, 'replaceCallback', $text);
function replaceCallback($match) {
if (is_array($match)) {
$htmlVersion = $match[1];
$urlVersion = urlencode($htmlVersion);
return '<a class="tag" rel="tag-definition" title="Click to know more about ' . $htmlVersion . '" href="?tag=' . $urlVersion . '">' . $htmlVersion . '</a>';
}
return $match;
}
错误消息指向preg_replace_Callback呼叫,说:
Warning: preg_replace_callback() [function.preg-replace-callback]: Unknown modifier 't' in /frontend.functions.php on line 43
HTML是不是一个正规的语言,所以正则表达式可能不是最好的工具在这里。 – 2010-06-29 09:00:33
您不应该使用正则表达式来解析html。看到这里:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Oded 2010-06-29 09:00:47
它会永远停止? – Gordon 2010-06-29 09:08:16