我想在浏览器上显示 PHP echo 语句的输出。结果是输出
htmlentities()
PHP的功能。
$str = "A 'quote' is <b>bold</b>";
// Expected Outputs: A 'quote' is <b>bold</b>
echo "<textarea>".htmlentities($str)."</textarea>";
// Expected Outputs: A 'quote' is <b>bold</b>
echo "<textarea>".htmlentities($str, ENT_QUOTES)."</textarea>";
显然,它给了我
A 'quote' is <b>bold</b>
在我的里面 <textarea>
请指教。
最佳答案
对其进行两次转义。
echo "<textarea>".htmlentities(htmlentities($str))."</textarea>";
htmlentities()
的目的是防止 HTML 按原样解析。但是你实际上想按原样显示 HTML 实体,所以你需要依次重新转义它们。
关于php - 在 PHP 中使用 htmlentities() 函数时防止浏览呈现 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3848664/