- strrev: documentation ( source)
<?php
$doc = new DOMDocument;
$xpath = new DOMXPath($doc);
$frag = $doc->createDocumentFragment();
$frag->appendXML(<<<'HTML'
<div id="article">
<p>Some text</p>
<p>More text</p>
<pre><code>
echo $variable;
</code></pre>
</div>
HTML
);
$text_nodes = $xpath->query('descendant::text()[not(ancestor::pre)]', $frag);
foreach ($text_nodes as $text_node) {
$text_node->data = strrev($text_node->data);
}
echo $doc->saveHTML($frag->firstChild);