I expect the code below to produce something like
“comment between script tags”
“comment outside script tags”
Instead it produces:
NULL
“comment outside script tags”
Assuming my XPATH query is correct, why can’t I parse the comment node inside the script tags?
Is there something I am missing?
<?php
$html = '
<script>
<!-- comment between script tags //-->
</script>
<!-- comment outside script tags //-->
';
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom); //put $dom into xpath
$xpath_query[] = '//script/comment()';
$xpath_query[] = '//comment()';
foreach($xpath_query as $xpath_query)
{
$match = $xpath->query($xpath_query)->item(0)->textContent; //query dom for match
echo '<pre>'; var_dump($match); echo '</pre><br />';
}
?>