How would I modify the script below so that if the first appearance of the keyword in the content string is already enclosed in bold or strong, I escape the node replacement?
$keyword = "test";
$content = "this is a <strong>test</strong> phrase with the word "test" in it.
in this example, nothing would be changed, since the first
appearance of the keyword is already in boldface";
@$d = new DOMDocument();
@$d->loadHTML($content);
@$x = new DOMXpath($d);
@$nodes = $x->query("//text()[contains(.,'$keyword') and not(ance开发者_JS百科stor::h1) and not(ancestor::h2) and not(ancestor::h3) and not(ancestor::h4) and not(ancestor::h5) and not(ancestor::h6) and not(ancestor::b) and not(ancestor::strong)]");
if ($nodes && $nodes->length) {
$node = $nodes->item(0);
// Split just before the keyword
$keynode = $node->splitText(strpos($node->textContent, $keyword));
// Split after the keyword
$node->nextSibling->splitText(strlen($keyword));
// Replace keyword with <b>keyword</b>
$replacement = $d->createElement('strong', $keynode->textContent);
$keynode->parentNode->replaceChild($replacement, $keynode);
}
echo $d->saveHTML();
In that specific case, use evaluate
instead of query
and change the XPath to count the elements that match the highlight criteria with
"count(//text()[contains(.,'$keyword') and (ancestor::b or ancestor::strong)])"
If that returns > 1
the keyword is already enclosed. You have to run this query before the other query.
精彩评论