Created
February 29, 2016 22:07
-
-
Save webmechanicx/e614d2a1e9f525178249 to your computer and use it in GitHub Desktop.
Get HTML,Text and Surrounded Tags
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 | |
$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