【问题标题】:How do I display a DOMElement?如何显示 DOMElement?
【发布时间】:2011-02-18 14:41:19
【问题描述】:

我正在使用readability 的代码从网页中提取 HTML。如何在页面上显示?

$content = grabArticle($webpage);
echo $content;

ERROR => Object of class DOMElement could not be converted to string...

【问题讨论】:

  • 你想展示什么?元素的内容?标签名称?包含元素的 HTML?

标签: php html dom


【解决方案1】:

Marc B 的回答让我走上了正轨,但我也有类似的需求,这就是我找到的代码:

$newdoc = new DOMDocument;
$node = $newdoc->importNode($node, true);
$newdoc->appendChild($node);
$html = $newdoc->saveHTML();

【讨论】:

    【解决方案2】:
    $content = grabArticle($webpage);
    
    $newdoc = new DOM;
    $newdoc->importNode($content);
    
    $html = $newdoc->saveHTML();
    

    这将基于您在grabArticle 中提取的节点创建一个新的完整HTML 文档。如果要将其插入另一个 HTML 页面,则需要去掉 DOM 插入的前导/尾随标签。

    【讨论】:

      【解决方案3】:

      是的,迈克给出的答案是正确的。这是一个简单的例子,采用

      节点找到并创建一个新文档。

      $newdoc = new DOMDocument;
      $nodes = $oldoc->getElementsByTagName('p');
      foreach($nodes as $node)
      {
          $newnode = $newdoc->importNode($node, true);
          $newdoc->appendChild($newnode);
      }
      //print the new html
      $html = $newdoc->saveHTML();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-16
        • 2016-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-25
        • 1970-01-01
        • 2010-12-23
        相关资源
        最近更新 更多