<?php
$response =
'<style><div id="subhead"></div></style>';
//echo $response;
$doc = new DOMDocument();
$doc->loadHTML($response);
$finder = new DomXPath($doc);
$term_select = $finder->query('//div[@id="subhead"]');
var_dump($term_select->item(0));
?>
The var_dump gets NULL, and I also get this Warning on line 8:
Warning: DOMDocument::loadHTML(): Unexpected end tag : div in Entity, line: 1 on line 8
Note that this is not my HTML (I’m scraping), so changing the HTML is not an option.
The problem is that you can’t have a DIV element instead a STYLE one so when you use
loadHTML, it fails to validate the HTML. If you did a$doc->saveHTML();you would have quickly realized that it’s wrapping the<div id="subhead">inCDATA.To solve the problem, use
loadXML()instead.