I have a problem to solve but I'm not able to do it, then I beg for your help!
In fact, it's all about a blog posting form. When they publish their articles, the post blog is converted by htmlentities an stored in the DB.
htmlentities(ucfirst($var), ENT_QUOTES, 'utf-8');
When it comes to display the text, they use the function html_entity_decode.
$var = html_entity_decode($var, ENT_QUOTES, 'UTF-8');
Now I want to be able to display tags within html tag < code > regardless the programming language used (PHP, HTML, Java, Javascript, ...).
For example I have the following code:
<?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;
Look at this image to see what I want as output:
As you can see, all tags are displayed as plain text except < br > tag
My thoughts:
I guess I have to parse this HTML code to find code tags and then convert codes within that tag in plain text, but I'm not sure to know how to this correctly.
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
foreach ($xpath->query("//code") as $node) {
$node = htmlspecialchars($node->nodeValue);
}
$texte = $dom->saveHTML();
echo $texte;
Can you please help me to achieve my objective?
question from:
https://stackoverflow.com/questions/66054768/how-to-parse-html-using-php-and-display-as-plain-text-all-tags-within-code-co 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…