i am using DOM to get content of div tag but inner html part is not shown.
Function is:
$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 source of page is (just for Div)
<div id="post">Some text <img src="..." /> <table>some codes</table></div>
then function returns just
"Some text "
but i want to get all HTML elements too, like that:
Some text <img src="..." /> <table>some codes</table>
Is there any way to do it? Thanks right now.
If you’re looking for the DOMDocument version of
innerHTMLin the browser DOM, the nearest issaveXML.(Remember to htmlspecialchars if you want that to actually appear as text.)
This gives you the
outerHTMLthough. If you really need theinnerHTML, you’d have to loop through each of the element’s child nodes and pass them tosaveXML, then implode them.And it’s XML serialisation only: there is no corresponding HTML version.
saveHTMLdoes exist but can only save the whole document at once, sadly. If it matters that you get legacy-HTML, you might be able to get away with it by passing in theLIBXML_NOEMPTYTAGoption to ensure that annoying empty tags like<script src="..."></script>don’t break the browser.