3v4l.org

run code in 500+ PHP versions simultaneously
<?php $pageHTML = '<html> <head></head> <body> <header> <div> <nav>Menu</nav> <span>Another text</span> </div> </header> <section>Section</section> <footer>Footer</footer> </body> </html>'; $dom = new DOMDocument; libxml_use_internal_errors(true); $dom->loadHTML($pageHTML); libxml_use_internal_errors(false); $xpath = new DOMXpath($dom); foreach($xpath->query('/html/body/header/div/*') as $i) { $i->setAttribute('class','my_class'); } echo $dom->saveHTML();
Output for 8.0.30, 8.1.22 - 8.1.34, 8.2.9 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head></head> <body> <header> <div> <nav class="my_class">Menu</nav> <span class="my_class">Another text</span> </div> </header> <section>Section</section> <footer>Footer</footer> </body> </html>
Output for 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.29, 8.1.0 - 8.1.21, 8.2.0 - 8.2.8
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><head></head><body> <header><div> <nav class="my_class">Menu</nav><span class="my_class">Another text</span> </div> </header><section>Section</section><footer>Footer</footer></body></html>

preferences:
103.63 ms | 1447 KiB | 4 Q