Keep line breaks

Victor posted 4 years ago

Hello everyone.

I add some text into mysql using textarea. I guess it keeps the text with CR/LF symbols, so when I display text with multiple lines I got them all in one string. I don't want to allow user to add any HTML markup, but I want to keep line breaks.

I have found this solution:

But is there any cleaner solution? (Except using pre tag) This one seems a little bit messy

JeroenVanOort replied 4 years ago Solution

I use {{ nl2br(e($text)) }}

Blackpig replied 4 years ago

I have a similar issue - I have a textarea that gets saved to the db. When I display it, I lose the line breaks. if I wrap with nl2br the break tag is displayed as text rather than rendered as a line break so my output looks like this:

this is some text<br />this is the next line

checking the source and the break tag is served as:


in my blade file I tried the following:

{{ $words }}  //No line breaks displayed
{{ nl2br($words )) }} // result as described above
{{ nl2br(e($words)) }} // result as described above

Any clues as to why this may be happenning?

Xum replied 4 years ago

It should be

{!! nl2br(e($words)) !!}
hairmenu replied 2 years ago

if you do so, be carreful with $words containing JS or so

pepijn replied 1 year ago

@hairmenu with e() that should be fixed info:

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us