Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3854848
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:41:09+00:00 2026-05-19T17:41:09+00:00

When I run the decorate_keyword() function below, I’m getting two errors that I’m trying

  • 0

When I run the decorate_keyword() function below, I’m getting two errors that I’m trying to resolve. The first seems the easiest to resolve. I’m getting…

Warning: DOMXPath::query()
[domxpath.query]: Invalid expression
in
C:\xampplite\htdocs\testsite\wp-content\plugins\myplugin\index.php
on line 48

Which points to the $nodes line in the function and is apparently stemming from the $keyword variable when it contains an empty space. $keyword = “test” does not throw the error. What do I need to change to overcome this error?

function rseo_decorate_keyword($postarray) {
    $keyword = "test";
    $content = $postarray['post_content'];
    /* 
     even though I can echo $content and get a string, 
     I'm getting error: Empty string supplied in loadHTML() when I use this. 
     so, I have to explicity set it to a string as below to test.
   */
    $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);
    }
     $postarray['post_content'] = $d;
return $postarray;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T17:41:09+00:00Added an answer on May 19, 2026 at 5:41 pm

    The contains XPath function should have the second argument in quotes if it contains various characters, such as ;&,. The space is one of them.

    $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)]");
    

    For your mysql_real_escape_string error, for some reason an object is being passed rather than a string. I’m not familiar with WordPress internals: you should make a separate question with the appropriate tags.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Run time error Cannot find column 0. below is my code string connectiostring =
It's my first time trying out C++ STL. I'm trying to build a multidimensional
I run a fairly active student blog that uses this event calendar plugin to
I run Flex Builder 3 on a mac and as my project grows -
I run a game and the running is done by hand, I have a
I run a rather complex project with several independent applications. These use however a
At run time I want to dynamically build grid columns (or another display layout)
I run my blog using Wordpress and all too recently became a big believer
We run full re-indexes every 7 days (i.e. creating the index from scratch) on
I run an ASP.NET website solution with a few other projects in it. I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.