How to parse HTML using PHP and display as plain text all tags within <code></code> except <br> tag

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?
Answer
Solution:
You can convert every node within <{-code-1}>{-code-1}{-code-1}> to plain text like this:
Havent found a way to preserve the formatting though.
<{-code-1}>$dom = new DOMDocument; $dom->loadHTML($html); $xpath = new DOMXPath($dom); foreach ($xpath->query("//{-code-1}") as ${-code-1}Node) { ${-code-1}Content = ''; while (${-code-1}Node->hasChildNodes()) { ${-code-1}Child = ${-code-1}Node->firstChild; ${-code-1}Content .= $dom->saveHTML(${-code-1}Child); ${-code-1}Node->removeChild(${-code-1}Child); } ${-code-1}Node->textContent = ${-code-1}Content; } $texte = $dom->saveHTML(); {-code-1}>
Working example.
Another possible solution is to replace the <{-code-1}>{-code-1}{-code-1}> element by an <{-code-1}>pre{-code-1}> element, which keeps the formatting:
<{-code-1}>foreach ($xpath->query("//{-code-1}") as ${-code-1}Node) { ${-code-1}Content = ''; foreach(${-code-1}Node->childNodes as ${-code-1}Child) { ${-code-1}Content .= $dom->saveHTML(${-code-1}Child); } $preformattedCodeNode->textContent = ${-code-1}Content; $preformattedCodeNode->setAttribute('style', ${-code-1}Node->getAttribute('style')); ${-code-1}Node->parentNode->replaceChild($preformattedCodeNode, ${-code-1}Node); } {-code-1}>
Working example.
Answer
Solution:
Do not usehtml_entity_decode()
if you want to display it in<code>
, you also need to replace newline\n
with<br>
and space
with
<?php
$code = <<<EOD
<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>
EOD;
$code = str_replace("\n","<br>", $code);
$code = str_replace(" "," ", $code);
$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%">
$code
</code>
<a href="">Link rendered</a>
</p>
EOD;
echo $html;
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function mysqli_connect()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.