嗨,即使有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);