Find & print the first div in an HTML document

929 Views Asked by At

I am trying to find the first div element in a remote page, but having difficulty. Here is what I have so far:

$url = "http://feed2all.eu/watch/193916/1/watch-skysports.html";
$html = file_get_contents($url);
$doc = new DOMDocument(); // create DOMDocument
libxml_use_internal_errors(true);

$doc->validateOnParse = true;
$doc->preserveWhiteSpace = false;
$doc->loadHTML($html); // load HTML you can add $html

$xpath = new DOMXpath($doc);
$nodes = $xpath->query( "//div");
foreach( $nodes as $node) {
    echo $node;
}

I've also tried to use:

$divs = $doc->getElementsByTagName('div');
foreach ($divs as $div) {
    echo $div;
}

Edit: how to echo the inner html of the got div

             $xpath = new DOMXpath($doc);
             $div = $xpath->query("//div[1]")->item(0);
             function get_inner_html( $div ) {
             $innerHTML= '';
             $children = $div->childNodes;
             foreach ($children as $child) {
             $innerHTML .= $child->ownerDocument->saveXML( $child );
              }
             echo $innerHTML;
              }

it give blank page

1

There are 1 best solutions below

2
On BEST ANSWER

If you want the first div use:

$div = $xpath->query("//div[1]")->item(0);

Also you cannot use echo to print a DOMElement. You can either print it's value:

echo $div->nodeValue;

or it's attributes:

echo $div->getAttribute('foo');

In comments you have asked for a way to get the innerHTML of the div. Here comes an example how to get the HTML of the first tag of this site:

$url = 'http://stackoverflow.com/questions/20600265/find-print-the-first-div-in-an-html-document?noredirect=1#comment30824495_20600265';

$doc = new DOMDocument();
@$doc->loadHTML(file_get_contents($url));
$selector = new DOMXPath($doc);

$div = $selector->query('//div[1]')->item(0);
var_dump($doc->saveHTML($div));