如何从具有自动换行符的textarea插入

时间:2018-01-20 20:06:38

标签: php html textarea newline line-breaks

我想在我的网站上创建一个帖子系统。

源是一个HTML textarea值,它插入MySQL数据库并显示如下:

<div align='center' style='background-color: #FFFFFF;text-align:left;width: 500px;padding: 10px;border-top: 5px solid #F45144;'>
<center><h1 style='color:#F45144;margin-top: -10;'>$Title</h1></center>
<p>
$Post
</div>
<br>
<style>br{line-height: 600%;}</style>

但文字格式不正确。 (&#34; $ Title&#34;和&#34; $ Post&#34;是来自数据库的2个变量)

看看图片: 当我插入这样的帖子时: enter image description here

我明白了: enter image description here

当我插入一个帖子时: enter image description here

结果如下: enter image description here

我想像img1一样插入并获得img4中的结果。 (无需插入<br>

1 个答案:

答案 0 :(得分:1)

因为您需要将文字的换行符转换为<br />标记, 您可以使用nl2br()函数实现此目的:

echo nl2br($text);