Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to echo an input of an textarea with line breaks?

Tags:

html

php

textarea

I'm using an textarea to send text to my DB.

Screenshot of db:

enter image description here

When I'm reading from the DB it removes the line breaks, how can I keep them in $row['opmerkingen']?

enter image description here

like image 318
Muiter Avatar asked Sep 07 '25 12:09

Muiter


1 Answers

When displaying text, use nl2br() to convert newlines to <br/> tags, i.e., instead of <?php echo $row['text']; ?>, use <?php echo nl2br($row['text']); ?>.

By default, browsers display newlines as spaces, therefore they have to be converted to <br/> tags.


For those who find this useful - please consider using white-space: pre-line, suggested by Emil Vikström. I'm not a web guy anymore and easily can't verify this, but Boaz says in comments that it is supported by all modern browsers. If so, that should be preferred to using nl2br().

like image 133
binaryLV Avatar answered Sep 09 '25 02:09

binaryLV