Prevent browse to render HTML when using htmlentities() function in PHP
I want to display the output of PHP echo statement on the browser. The result is output of 
htmlentities() function of PHP.
$str = "A 'quote' is <b>bold</b>";
// Expected Outputs: A 'quote' is <b>bold</b>
echo "<textarea>".htmlentities($str)."</textarea>";
//开发者_JAVA技巧 Expected Outputs: A 'quote' is <b>bold</b>
echo "<textarea>".htmlentities($str, ENT_QUOTES)."</textarea>";
Apparently, it is giving me
A 'quote' is <b>bold</b> inside my <textarea>
Please advise.
Double-escape it.
echo "<textarea>".htmlentities(htmlentities($str))."</textarea>";
The purpose of htmlentities() is to prevent HTML being parsed as-is. But you actually want to display the HTML entities as-is, so you need to re-escape them in turn.
BalusC's solution will work, or you could just write your string as you want it to appear and continue to use htmlentities only once:
    $str = "A 'quote' is <b>bold</b>";
    echo "<textarea>".htmlentities($str)."</textarea>";
    // Expected Outputs: A 'quote' is <b>bold</b>
    // Actual Output   : A 'quote' is <b>bold</b>
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论