loadHTML()上的DOMDocument错误“作为输入提供的空字符串”?

时间:2011-02-02 18:07:06

标签: php domdocument

当我从下面的$ d,$ x DOMdocument变量中删除@符号时,我收到错误...

  

警告:DOMDocument :: loadHTML()   [domdocument.loadhtml]:空字符串   作为输入提供   C:\ Xampplite文件\ htdocs中\ mysite的\可湿性粉剂内容\插件\为myplugin \的index.php   第50行

在$ content变量上,当我运行下面的函数时。即使我可以回显$ content并得到一个字符串。我错过了什么?

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($replacement, $keynode);
    }
     $postarray['post_content'] = $d;
return $postarray;
}

1 个答案:

答案 0 :(得分:0)

您应该输入http://www.example.com之类的网址字符串到loadHTML()而不是数组。