How to display DOMElement?

I am using readability code to extract HTML from a web page. How to display it on the page?

$content = grabArticle($webpage); echo $content; ERROR => Object of class DOMElement could not be converted to string... 
+4
source share
3 answers
 $content = grabArticle($webpage); $newdoc = new DOM; $newdoc->importNode($content); $html = $newdoc->saveHTML(); 

This will create a new full node based HTML document that you extracted in grabArticle. If you paste this into another HTML page, you will need to remove the top / trailing tags that the DOM inserts.

+5
source

Marc B's answer gave me the correct path, but I had a similar need, and that was the code I landed on:

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

Yes, the answer Mike gave is correct. Here is a simple example that accepts found nodes

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(); 
+3
source

Source: https://habr.com/ru/post/1309554/


All Articles