Case insensitive text() comparison with DOMdocument?
How can I do a case insensitive comparison on the keyword's appearance in my content in the script below?
If I use this...
$keyword = strtolower(rseo_getKeyword($post));
$nodes = $x->query("//text()[
contains(
translate(.,'ABCDEFGHJIKLMNOPQRSTUVWXYZ',
'abcdefghjiklmnopqrstuvwxyz'),
'$keyword')
The replacement is only made on keyword matches within the content that is already lowercase. It does not appear to be doing a case insensitive lookup.
$keyword = rseo_getKeyword($post);
$content = $postarray['post_content']; //error: Empty string supplied in loadHTML() when I use this.
//$content = "this is a test phrase";
@$d = new DOMDocument();
@$d->loadHTML($content);
@$x = new DOMXpath($d);
@$nodes = $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)]");
if ($nodes && $nodes->length) {
$node = $n开发者_开发问答odes->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('b', $keynode->textContent);
$keynode->parentNode->replaceChild($replacement, $keynode);
}
echo $d->saveHTML();die;
//text()
[contains(translate(.,'ABCDEFGHJIKLMNOPQRSTUVWXYZ',
'abcdefghjiklmnopqrstuvwxyz'),
'$keyword')
]
The correct expression must test if the lowercased text contains the lowercased keyword:
//text()
[contains(translate(.,'ABCDEFGHJIKLMNOPQRSTUVWXYZ',
'abcdefghjiklmnopqrstuvwxyz'),
translate('$keyword','ABCDEFGHJIKLMNOPQRSTUVWXYZ',
'abcdefghjiklmnopqrstuvwxyz')
)
]
The text()
function returns all text node children of the context node. When you call it as a parameter to translate()
, the context node is a text node and so will have no text node children. Instead, use .
to properly select the context node itself as you really want.
Replace your try:
contains(translate(text(), 'ABC…
with
contains(translate(., 'ABC…
精彩评论