我想在浏览器上显示PHP echo语句的输出。结果是输出
PHP的htmlentities()
功能。
$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>
请告知。
答案 0 :(得分:4)
双击它。
echo "<textarea>".htmlentities(htmlentities($str))."</textarea>";
htmlentities()
的目的是防止按原样解析HTML。但实际上你想要按原样显示HTML实体,所以你需要依次重新转义它们。
答案 1 :(得分:1)
BalusC的解决方案可以使用,或者您可以根据需要编写字符串并继续只使用htmlentities
一次:
$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>