How to display XML in HTML in PHP?

后端 未结 5 1934
南笙
南笙 2020-11-28 10:40

I have a string with XML:

$string = 
\"

    
       Shoue
    

\";
<         


        
相关标签:
5条回答
  • 2020-11-28 11:01

    It should work like that:

    echo '<p>This is XML string content:</p>'
    echo '<pre>';
    echo htmlspecialchars($string);
    echo '</pre>';
    
    0 讨论(0)
  • 2020-11-28 11:02

    you can use htmlentities(), htmlspecialchars() or some similar function.

    0 讨论(0)
  • 2020-11-28 11:11

    If it is a SimpleXMLobject

    <pre>
    <?php
    echo htmlspecialchars(print_r($obj,true));
    ?>
    </pre>
    
    0 讨论(0)
  • 2020-11-28 11:13

    If you just want a plain-text representation of your (pre-formatted) string, you can wrap it in HTML <pre/> tags and use htmlentities to escape the angle brackets:

    <?PHP echo '<pre>', htmlentities($string), '</pre>'; ?>
    0 讨论(0)
  • 2020-11-28 11:17

    I searched for a solution to have slightly coloured output:

    $escaped = htmlentities($content);
    $formatted = str_replace('&lt;', '<span style="color:blue">&lt;', $escaped);
    $formatted = str_replace('&gt;', '&gt;</span>', $formatted);
    echo "<pre>$formatted</pre>\n";
    
    0 讨论(0)
提交回复
热议问题