Nitesh morajkar Nitesh morajkar - 3 years ago 1005
PHP Question

Get entire BODY content using PHP DOM DOCUMENT

I want to get entire body tag content using DOM Document.

I used following code:

$dom = new domDocument;

/*** load the html into the object ***/

/*** the table by its tag name ***/
$tables = $dom->getElementsByTagName('body')->item(0)->nodeValue;

This gives me TExt. I want entire body content.

Answer Source

You can pass the body DOMElement to either DOMDocument::saveHTML() or DOMDocument::saveHTMLFile(), e.g.

$doc = new DOMDocument;

$body = $doc->getElementsByTagName('body');
if ( $body && 0<$body->length ) {
    $body = $body->item(0);
    echo $doc->savehtml($body);


Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : p in, line: 2843 [...]
<body class="home-page">
<noscript><div id="noscript-padding"></div></noscript>
<div id="notify-container"></div>
<div id="overlay-header"></div>
<div id="custom-header"></div>
<div class="container">
        <div id="header">
            <div id="portalLink">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download