开发者

How do I display a DOMElement?

I'm using readability's code to extract HTML from a web page. How do I display it on a page?

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

ERROR => Object of class DOMElement could not be converted to 开发者_高级运维string...


Marc B's answer got me on the right track, but I had a similar need and this was the code I landed on:

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


$content = grabArticle($webpage);

$newdoc = new DOM;
$newdoc->importNode($content);

$html = $newdoc->saveHTML();

That'll creat a new complete HTML document based on the node you extracted in grabArticle. If you're inserting that into another HTML page, you'll need to strip off the leading/trailing tags that DOM inserts.


Yes the answer that Mike gave is correct. Here is a simple example that takes the

nodes found and creates a new document.

$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();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜