Retrieving div content with PHP DOM

I looked at other Stackoverflow questions on this topic and none of the suggested solutions work for me.

I have an html page (scraped with file_get_contents() ), and in this html there is a div with the identifier "main" - I need to get the contents of this div with PHP DOMDocument or something similar. In this situation, I cannot use the SimpleHTMLDom parser, which complicates the situation a bit.

+6
source share
2 answers

Option DOMDocument + XPath:

 $xml = new DOMDocument(); $xml->loadHtml($temp); $xpath = new DOMXPath($xml); $html = ''; foreach ($xpath->query('//div[@id="main"]/*') as $node) { $html .= $xml->saveXML($node); } 

If you are looking for innerHTML() (PHP DOMDocument help question) - instead of innerXML() , as in this answer - the xpath related option is provided in this answer .

Here the adoption with changes is emphasized:

 $html = ''; foreach ($xpath->query('//div[@id="main"]/node()') as $node) ###### { $html .= $xml->saveHTML($node); #### } 
+5
source

Using DOMDocument ...

 $dom = new DOMDocument; $dom->loadHTML($html); $main = $dom->getElementById('main'); 

To get serialized HTML ...

 html = ''; foreach($main->childNodes as $node) { $html .= $dom->saveXML($node, LIBXML_NOEMPTYTAG); } 

Use saveHTML() if your version of PHP supports it.

+3
source

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


All Articles