简体   繁体   中英

How to preserve line breaks in textarea?

I went through similar questions on this issue but couldn't find a solution. I have a simple textarea in which text will be pasted or entered directly.

How do I preserve those line breaks?

Right now line breaks will just be ignored, but when I print_r($_POST) and inspect the output in the Chrome Developer Console it still has the original text breaks. How can I display those when showing the submitted data?

Right now line breaks will just be ignored

No, it is still there. The key you miss is that what you enter goes as ASCII code LF ( 0xA ) but this makes no effect in HTML unless the text is displayed as preformatted block ( <pre> ... </pre>). If not, then line break is represented by <pre> ... </pre>). If not, then line break is represented by
tag so what you need to do if you want to display entered text is to convert LF s to
(you can use nl2br() function to do that) or use ` block.

正如Marcin所指出的,按照此答案中的指示显示数据时,请使用nl2br()https : //stackoverflow.com/a/6480671/6206601

You can use nl2br() function:

Inserts HTML line breaks before all newlines in a string

echo(nl2br($_POST));

More information at http://php.net/manual/en/function.nl2br.php .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM