3v4l.org

run code in 500+ PHP versions simultaneously
<?php $html = <<<HTML <div class="content"> <h2>Hello World</h2> <p>This is a paragraph.</p> </div> HTML; libxml_use_internal_errors(true); $dom = new DOMDocument('1.0', 'UTF-8'); $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); libxml_clear_errors(); // Change the heading text $h2 = $dom->getElementsByTagName('h2')->item(0); $h2->nodeValue = 'Updated Heading'; // Add a class to the paragraph $p = $dom->getElementsByTagName('p')->item(0); $p->setAttribute('class', 'intro'); // Output the modified HTML echo $dom->saveHTML();
Output for 8.2.31 - 8.2.33, 8.3.0 - 8.3.33, 8.4.1 - 8.4.24, 8.5.0 - 8.5.9
<div class="content"> <h2>Updated Heading</h2> <p class="intro">This is a paragraph.</p> </div>

preferences:
58.02 ms | 787 KiB | 4 Q