在下面的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