为什么 PHP 回显的文本会丢失其格式?

2022-01-12 00:00:00 formatting php

任何想法为什么来自 DB 的格式化文本,当在 php 中回显时会丢失其格式,即没有新行?谢谢!

Any ideas why formatted text from DB, when echo-ed out in php loses its formatting, i.e. no new lines? Thanks!

推荐答案

使用nl2br().

浏览器会忽略新行.这就是为什么您会看到所有没有换行符的文本.nl2br() 将新行转换为 <br/> 标记,显示为浏览器中的新行.

New lines are ignored by browser. That's why you see all text without line breaks. nl2br() converts new lines to <br /> tags that are displayed as new lines in browsers.

如果你想在 <textarea> 中显示你的文本,你不需要把所有的新行都转换成 <br/>.无论如何,如果你这样做......你会在换行的地方看到<br/>"作为文本.

If you want to display your text in <textarea>, you don't need to convert all new lines to <br />. Anyway, if you do it... you will see "<br />"s as text in new lines places.

相关文章