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();
精彩评论