Take the 2-minute tour ×

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));


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();
    @$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)]");
        $node = $nodes->item(0);
        // Split just before the keyword
        // Split after the keyword
        $replacement = $d->createElement('b', $keynode->textContent);
    echo $d->saveHTML();die;
share|improve this question
Pretty much duplicates stackoverflow.com/questions/625986/… –  rik Feb 2 '11 at 18:38
@rik, I've attempted to substitute the translate routing into my xquery (and updated my question with that info) but when I do, the replacement is not made at all. –  Scott B Feb 2 '11 at 19:37
possible duplicate of case insensitive xpath searching in php –  Gordon Feb 2 '11 at 23:23
Good question, +1. See my answer for a complete and easy solution. –  Dimitre Novatchev Feb 3 '11 at 5:11

2 Answers 2

up vote 2 down vote accepted

The correct expression must test if the lowercased text contains the lowercased keyword:

share|improve this answer
+1. Full and classic. Missing golden badge is near :) –  Flack Feb 3 '11 at 8:14

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…


contains(translate(., 'ABC…
share|improve this answer
I've changed it in my question, but I'm still doing something wrong. I'm only getting matches when the keyword is lowercase within the content. –  Scott B Feb 2 '11 at 21:03
@Scott B, maybe provide a reproducible example and we can move from there. Upper/lower/MixEd case keywords in the content work fine with the short snippet of HTML I used just to check the notes above. –  salathe Feb 2 '11 at 21:20

Your Answer


