在下面的foreach循环,什么是正确的语法,以回报的关键字只有第一个实例,其包装以粗体标签,然后退出循环和功能?DOM文档的foreach更换
例如,关键字是“蓝色小工具”。所以我想在字符串(在$内容)的首次亮相,从蓝色小工具变为
<b>blue widgets</b>
下面是我使用的解析内容的程序。...
function sx_decorate_keyword($content){
$keyword = "blue widgets";
$d = new DOMDocument();
$d->loadHTML($content);
$x = new DOMXpath($d);
foreach($x->query("//text()[
contains(.,$keyword')
and not(ancestor::h1)
and not(ancestor::h2)
and not(ancestor::h3)
and not(ancestor::h4)
and not(ancestor::h5)
and not(ancestor::h6)]") as $node){
//need to wrap bold tags around the first instance of the keyword, then exit the routine
}
return $content;
}
刚古玩,为什么不使用preg_replace? – Dmitri 2011-01-28 18:59:28