3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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);
based on Cae2m
Output for 5.6.0 - 5.6.30, hhvm-3.12.14 - 3.17.3, 7.0.0 - 7.3.0alpha1
mysection container message