Retrieving div content (including child tags) using the DOM

I am using the DOM to retrieve the contents of a div tag, but the inside of the html is not shown. Function:

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTMLFile("$url");
libxml_use_internal_errors(false);
$xpath = new DOMXPath($dom);
$divTag = $xpath->query('//div[@id="post"]');
foreach ($divTag as $val) {
echo $val->getAttribute('title') . ' - ' . $val->nodeValue . "<br />\n";
}

if page source (Div only)

<div id="post">Some text <img src="..." /> <table>some codes</table></div>

then the function only returns

"Some text " 

but I want to get all the HTML elements, for example:

Some text <img src="..." /> <table>some codes</table>

Is there any way to do this? Thanks right now.

+3
source share
3 answers

If you are looking for a version of DOMDocument innerHTMLin your browser DOM, the closest saveXML.

echo $dom->saveXML(val).'<br />\n';

(Remember htmlspecialchars if you want this to really display as text.)

outerHTML. innerHTML, saveXML, .

XML-: HTML. saveHTML , , . , HTML-, , LIBXML_NOEMPTYTAG, , , <script src="..."></script>, .

+2

Basically what bobince said, but I add that you can use output buffering to get the content if you do this in php without displaying it in html.

$divTag = $xpath->query('//div[@id="post"]');
ob_start();
foreach ($divTag as $val) {
   echo $dom->saveXML($val);
}
$content = ob_get_clean();
0
source

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


All Articles