<?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);
foreach ($xpath->query("//code") as $codeNode) {
$codeContent = '';
while ($codeNode->hasChildNodes()) {
$codeChild = $codeNode->firstChild;
$codeContent .= $dom->saveHTML($codeChild);
$codeNode->removeChild($codeChild);
}
$codeNode->textContent = $codeContent;
}
$texte = $dom->saveHTML();
echo $texte;
<!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>
<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> </body></html>
<!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>
<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></body></html>