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 = $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('b', $keynode->textContent);
$keynode->parentNode->replaceChild($replacement, $keynode);
}
echo $d->saveHTML();die;