<?php $content = <<<_HTML <!doctype html><html><head><title>What</title></head><body><p>(what)</p><div id="main"><p><span>what?</span></p><ul><li><span>foo</span> what!</li><li>(what?)</li></ul></div><p>(what)</p></body></html> _HTML; $find = ['(', ')', '?', '!']; $replace = ['( ', ' )', ' ?', ' !']; $selector = implode(' or ', array_map(function($char) { return sprintf('contains(text(), "%s")', $char); }, $find)); $query = sprintf('//div[@id="main"]//*[%s]/text()', $selector); $doc = new DOMDocument(); $doc->loadHTML($content); $xpath = new DOMXPath($doc); $elements = $xpath->query($query); foreach ($elements as $element) { $element->nodeValue = html_entity_decode(str_replace($find, $replace, $element->nodeValue)); } echo $doc->saveHTML();
You have javascript disabled. You will not be able to edit any code.