首页 > 解决方案 >
即使有 htmlspecialchars 功能也能正常工作

问题描述

<br>嗨,即使有功能,有什么方法可以使工作htmlspecialchars。我的问题是,当我在函数htmlspecialchars中放置文本时,即使文本有新行,也没有新行。我的代码是这样的:

$text_val = "
this is a line
it is a new line
one more line
";

$final_text = str_replace("\n","<br>",$text_val);

$text = htmlspecialchars($final_text);
echo $text;

结果是这样的:

this is a line it is a new line one more line

但我希望它像第一个:

this is a line
it is a new line
one more line

谢谢你们。

标签: phphtml

解决方案


使用内置的 PHP 函数nl2br $final_text = nl2br($text_val);


推荐阅读