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
If you want the first div use:
Also you cannot use
echo
to print aDOMElement
. You can either print it's value:or it's attributes:
In comments you have asked for a way to get the
innerHTML
of thediv
. Here comes an example how to get the HTML of the first tag ofthis
site: