line breaks in a textarea
Don't do nl2br
when you save it to the database. Do nl2br
when you're displaying the text in HTML. I can strongly recommend to not store any HTML formatting in the database (unless you're using a rich HTML editor as well, in which case it would be silly not to).
A newline \n
will just become a newline in the textarea.
You could use str_replace
to replace the <br />
tags into end of line characters.
str_replace('<br />', PHP_EOL, $textarea);
Alternatively, you could save the data in the database without calling nl2br
first. That way the line breaks would remain. When you display as HTML, call nl2br
. An additional benefit of this approach is that it would require less storage space in your database as a line break is 1 character as opposed to "<br />
" which is 6.
Ahh, it is really simple
just add
white-space:pre-wrap;
to your displaying element css
I mean if you are showing result using <p>
then your css should be
p{ white-space:pre-wrap;}