Last active
September 9, 2020 06:51
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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