SparklingWater
SparklingWater

Reputation: 368

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?

Upvotes: 0

Views: 1793

Answers (3)

nbarbosa
nbarbosa

Reputation: 1712

As Marcin noted, use nl2br() when displaying your data as instructed in this answer: https://stackoverflow.com/a/6480671/6206601

Upvotes: 1

Panda
Panda

Reputation: 6896

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.

Upvotes: 1

Marcin Orlowski
Marcin Orlowski

Reputation: 75635

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
tag so what you need to do if you want to display entered text is to convertLFs to
(you can usenl2br()function to do that) or use` block.

Upvotes: 1

Related Questions