3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = <<<HTML <!doctype html> <html> <body> text to keep <span xxx="xxxx"><span xxx="xxx">unwanted text</span> unwanted text </span> text to keep </body> </html> HTML; $doc = new DOMDocument(); $doc->loadHTML($str); $xpath = new DOMXPath($doc); foreach($xpath->evaluate("//span") as $node) { echo 'Removing: '.$node->nodeValue."<br>"; $node->parentNode->removeChild($node); } $output = $doc->saveHTML(); echo htmlspecialchars($output);

preferences:
24.24 ms | 404 KiB | 5 Q