아래 foreach 루프에서 키워드의 첫 번째 인스턴스 만 반환하고 굵게 태그로 묶은 다음 루프와 함수를 종료하려면 올바른 구문은 무엇입니까?DOMDocument foreach replacement
예를 들어, 키워드는 "blue widgets"입니다. 그래서
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
@Dmitri - 일부분도 아닙니다. –
@Dmitri : "제목 태그에 없습니다"예외와 함께 preg_replace를 사용하여 예제를 제공 할 수 있습니까? –