如何使<br>工作即使有htmlspecialchars功能也



嗨,即使有htmlspecialchars功能,也有办法使<br>工作。 我的问题是当我在函数中放入文本时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

谢谢你们。

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

您正在寻找htmlspecialchars_decode()函数,但改用htmlspecialchars()函数。

替换此行:

$text = htmlspecialchars($final_text);

有了这个:

$text = htmlspecialchars_decode($final_text);

最新更新