<?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();
You have javascript disabled. You will not be able to edit any code.