- array_unshift: documentation ( source)
- libxml_clear_errors: documentation ( source)
- libxml_use_internal_errors: documentation ( source)
<?php
$str = <<<'AHA'
<section debug = 'mysection'>
<div>
<div id = 'current' debug ='container'>
<p id = 'last' debug = 'message'>
</p>
</div>
</div>
</section>
AHA;
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($str);
libxml_clear_errors();
$node = $doc->getElementById('last');
function getDebugNodes($node) {
while($node = $node->parentNode){
if($node->nodeType===1 AND$node->hasAttribute('debug')){
$snake[] = $node;
}
}
return $snake;
}
//echo "\n" . $node->getAttribute('name') . $node->getAttribute('debug');
function make($snake, $index) {
$path = '';
for($i = $index; $i < count($snake) - $index; $i++) {
$path = "{$snake[$i]->getAttribute('debug')} $path";
}
return $path;
}
$snake = getDebugNodes($node);
array_unshift($snake, $node);
//foreach($snake as $element){
// echo ' ' . $element->getAttribute('debug');
//}
echo "\n" . make($snake, 0);