given the following string in PHP:
$html = \"
text 1
The DOMDocument class is a very straight-forward and easy-to-understand interface designed to assist you in working with your data in a DOM-like fashion. Querying your DOM with xpath selectors should be the task(s) all the more trivial:
// Build our DOMDocument, and load our HTML
$doc = new DOMDocument();
$doc->loadHTML($html);
// Preserve a reference to our DIV container
$div = $doc->getElementsByTagName("div")->item(0);
// New-up an instance of our DOMXPath class
$xpath = new DOMXPath($doc);
// Find all elements whose class attribute has test2
$elements = $xpath->query("//*[contains(@class,'test2')]");
// Cycle over each, remove attribute 'class'
foreach ($elements as $element) {
// Empty out the class attribute value
$element->attributes->getNamedItem("class")->nodeValue = '';
// Or remove the attribute entirely
// $element->removeAttribute("class");
}
// Output the HTML of our container
echo $doc->saveHTML($div);