Skip to content

Instantly share code, notes, and snippets.

@webmechanicx
Created February 29, 2016 22:07
Show Gist options
  • Save webmechanicx/e614d2a1e9f525178249 to your computer and use it in GitHub Desktop.
Save webmechanicx/e614d2a1e9f525178249 to your computer and use it in GitHub Desktop.
Get HTML,Text and Surrounded Tags
<?php
$html = <<< HTML
<body>
<h1>Hello,
<b>world!</b>
</h1>
</body>
HTML;
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$textNodes = array();
foreach($xpath->query('/html/body//text()') as $i => $textNode) {
$textNodes[$i] = array(
'text' => $textNode->nodeValue,
'parents' => array()
);
for (
$currentNode = $textNode->parentNode;
$currentNode->parentNode;
$currentNode = $currentNode->parentNode
) {
$textNodes[$i]['parents'][] = $currentNode->nodeName;
}
}
print_r($textNodes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment