3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<EOD <h1>Escape HTML or Other Programming tags</h1> <p>This must be rendered without any problem</p> <p> <code style="display:block;background:rgb(230,230,230);padding:2%"> <h4>Show this title in HTML</h4> <p>This paragraph must be in <strong>HTML</strong> and this <a href="">Link</a> too !</p> <?php echo "Display this PHP code!"; ?> <script> alert("Don't pop-up please!"); </script> </code> <a href="">Link rendered</a> </p> EOD; $dom = new DOMDocument; $dom->loadHTML($html); $xpath = new DOMXPath($dom); $preformattedCodeNode = $dom->createElement('pre'); foreach ($xpath->query("//code") as $codeNode) { $codeContent = ''; foreach($codeNode->childNodes as $codeChild) { $codeContent .= $dom->saveHTML($codeChild); } $preformattedCodeNode->textContent = $codeContent; $preformattedCodeNode->setAttribute('style', $codeNode->getAttribute('style')); $codeNode->parentNode->replaceChild($preformattedCodeNode, $codeNode); } $texte = $dom->saveHTML(); echo $texte;
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.26, 8.1.0 - 8.1.13
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><h1>Escape HTML or Other Programming tags</h1> <p>This must be rendered without any problem</p> <p> <pre style="display:block;background:rgb(230,230,230);padding:2%"> &lt;h4&gt;Show this title in HTML&lt;/h4&gt; &lt;p&gt;This paragraph must be in &lt;strong&gt;HTML&lt;/strong&gt; and this &lt;a href=""&gt;Link&lt;/a&gt; too !&lt;/p&gt; &lt;?php echo "Display this PHP code!"; ?&gt;&lt;script&gt; alert("Don't pop-up please!"); &lt;/script&gt;</pre> <a href="">Link rendered</a> </p></body></html>

preferences:
66.34 ms | 411 KiB | 5 Q