开发者

DOMDocument error on loadHTML() "Empty string supplied as input"?

开发者 https://www.devze.com 2023-02-08 17:47 出处:网络
When I remove the @ sign from my $d, $x DOMdocument variables below, I\'m getting the error... Warning: DOMDocument::loadHTML()

When I remove the @ sign from my $d, $x DOMdocument variables below, I'm getting the error...

Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Empty string supplied as input in C:\xampplite\htdocs\mysite\wp-content\plugins\myplugin\index.php on line 50

On the $content variable, when I run the function below. Even though I can echo $content and get a string. What am I missing?

add_filter('wp_insert_post_data', 'decorate_keyword');

function decorate_keyword($postarray) {
    global $post;
    $keyword = getKeyword($post);
    /*
      Even though I can echo $content, I'm getting the error referenced above.
      I have to explicitly set it to a string to overcome the error.
    */
    $content = $postarray['post_content']; 
    //$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($replacemen开发者_如何学Pythont, $keynode);
    }
     $postarray['post_content'] = $d;
return $postarray;
}


You should input a URL string like http://www.example.com to loadHTML() instead of an array.

0

精彩评论

暂无评论...
验证码 换一张
取 消