Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get entire BODY content using PHP DOM DOCUMENT

Tags:

php

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

I used following code:

$dom = new domDocument;

/*** load the html into the object ***/
$dom->loadHTML($html);

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

This gives me TExt. I want entire body content.

like image 603
Nitesh morajkar Avatar asked Jan 23 '12 07:01

Nitesh morajkar


People also ask

Can we use DOM in PHP?

The built-in DOM parser makes it possible to process XML documents in PHP.

What is DOM document in PHP?

The DOMDocument::getElementsByTagName() function is an inbuilt function in PHP which is used to return a new instance of class DOMNodeList which contains all the elements of local tag name.

What is loadHTML?

DOMDocument::loadHTMLThe function parses the HTML contained in the string source . Unlike loading XML, HTML does not have to be well-formed to load. This function may also be called statically to load and create a DOMDocument object.


1 Answers

$dom = new domDocument;
$dom->loadHTML($html);

// ... change, replace ...
// ... mock, traverse ..

$body = $dom->documentElement->lastChild;
$dom->saveHTML($body);
like image 170
Spooky Avatar answered Oct 05 '22 20:10

Spooky