我想通过在发送输出之前处理输出来执行搜索字的服务器端突出显示。我想要替换所有没有包含在内的字符串< ... >
后面 “服务器端” 的理由是:
1)投票高亮Unicode文本很烂。 2)\ b不使用unicode(至少在JS,AFAIK中)。 3)JS中没有后援支持。
我正在使用下面的功能,但昨晚意识到,第一部分被写入跳过< ...>不工作。
public function ss_highlight($terms, $buf)
{
if (empty($terms)) {
return $buf;;
}
/* sort before using length for better match */
usort($terms, function($a, $b) {
return mb_strlen($b) - mb_strlen($a);
});
$str_terms = implode('|', $terms);
/* server side highlighter */
$buf = preg_replace('/(<[^>]+>)*(?<=[\s|:|\-|>|\(|\)|\.|,|\/|^])('.$str_terms.')(?=[\s|:|\-|<|\(|\)|\.|,|\/]|$)/i', '$1<span class="highlight">$2</span>', $buf);
return $buf;
}
任何想法,将不胜感激。
问候。 PS:我在Replacing all occurences of a specific word which are not enclosed with the words OPEN and CLOSE?中看到过一些类似的东西,但无法弄清楚如何使这符合我的要求。
我知道,但我不知道如何使用DOMDocument来实现这个结果。仍试图了解DOMDocument类如何工作。 – bilalhp 2013-03-18 10:50:21