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();

preferences:
51.7 ms | 789 KiB | 5 Q