Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

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: show tags within HTML code tag as plain text

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can convert every node within code to plain text like this:

Havent found a way to preserve the formatting though.

$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();

Working example.


Another possible solution is to replace the code element by an pre element, which keeps the formatting:

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);
}

Working example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...