我试图简单地替换一些新行并尝试了三种不同的方法,但我没有得到任何改变:
$description = preg_replace('/\r?\n|\r/', '<br/>', $description);
$description = str_replace(array("\r\n", "\r", "\n"), "<br/>", $description);
$description = nl2br($description);
这些都应该有效,但我仍然得到换行符。它们是双重的:“\r\r”。那不应该让这些失败,对吧?
原文由 theflowersoftime 发布,翻译遵循 CC BY-SA 4.0 许可协议
已经有
nl2br()
函数在换行符前 插入<br>
标签:示例( 键盘):
但如果它仍然不起作用,请确保文本
$desciption
是双引号。这是因为与双引号字符串相比,单引号不会“扩展”转义序列,例如
\n
。引用自 PHP 文档: