Skip to content

Instantly share code, notes, and snippets.

@ik5
Last active September 9, 2020 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ik5/34f7cd7c274c8f5ae4df31773b961eb0 to your computer and use it in GitHub Desktop.
Save ik5/34f7cd7c274c8f5ae4df31773b961eb0 to your computer and use it in GitHub Desktop.
A simple example on reading a PHP <head> tag and print it's content including attributes
<?php
$doc = new DOMDocument();
$doc->loadHTMLFile('test.html', LIBXML_NOWARNING | LIBXML_NOERROR);
$nodes = $doc->getElementsByTagName('head');
if ($nodes->length <= 0) {
die('no head found');
}
for ($i =0; $i < $nodes->length; $i++) {
$item = $nodes->item( $i );
if ($item->childNodes->length <= 0) {
continue;
}
for ($j = 0; $j < $item->childNodes->length; $j++) {
$childItem = $item->childNodes->item($j);
echo $childItem->nodeName;
echo ' = ';
echo $childItem->nodeValue;
if (!$childItem->hasAttributes()) {
continue;
}
for ($a = 0; $a < $childItem->attributes->length; $a++) {
$attr = $childItem->attributes->item($a);
echo "\t";
echo $attr->nodeName;
echo ' = ';
echo $attr->nodeValue;
echo "\n";
}
echo "\n";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment