3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<HTML <body class="old_attr"> <div>(content)</div> </body> HTML; $new_class = "new_class_attr"; $dom=new DOMDocument; $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $body = $dom->getElementsByTagName('body')->item(0); if ($body->hasAttribute("class")) { $new_class = $body->getAttribute("class") . " " . $new_class; } $body->setAttribute("class", $new_class); echo $dom->saveHTML();
Output for 5.6.38, 7.1.0 - 7.1.24, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.26, 8.0.0 - 8.0.13, 8.1rc1 - rc3
<body class="old_attr new_class_attr"> <div>(content)</div> </body>

preferences:
36.22 ms | 460 KiB | 5 Q