3v4l.org

run code in 300+ PHP versions simultaneously
<?php function reverseVisibleText($doc, $node) { $parts = []; foreach ($node->childNodes as $child) { if ($child->nodeType === XML_TEXT_NODE) { array_unshift($parts, strrev($child->nodeValue)); } elseif ($child->nodeType === XML_ELEMENT_NODE) { $child->nodeValue = reverseVisibleText($doc, $child); array_unshift($parts, $doc->saveHTML($child)); } } return implode($parts); } $html = 'The quick <font color="brown">brown</font> fox jumps over the lazy dog'; $doc = new DOMDocument(); $doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); echo reverseVisibleText($doc, $doc->documentElement);
Output for 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
god yzal eht revo spmuj xof <font color="brown">nworb</font> kciuq ehT

preferences:
55.81 ms | 992 KiB | 4 Q