Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ahoi's avatar
Level 5

Keep line breaks from text area

Hi there,

I got a textarea called intro. Now I'd like to render the content, by using this:

{{ nl2br(e(auth()->user()->intro)) }}

But now, the text is shown like that:

Hello!<br /> <br /> World...Hello World...

If I just use {{ auth()->user()->intro }} the text is shown without line breaks at all.

How can I get the output:

Hello!


World...Hello World...

?

Thanks for any help :-)

0 likes
4 replies
cmdobueno's avatar
Level 18

You look like you want to use something more like a textboxio (or fill in the blank for your preffered wysiwyg text edit), or tinymce.

1 like
click's avatar

A simple google search with Laravel nl2br blade would give you many results. But here you go:

{!! nl2br(e($text)) !!}

you first escape the raw value, than you turn the new lines into <br /> tags and you do not render the variable with {{ but with {!! to disable the automatic escaping of blade.

1 like
ahoi's avatar
Level 5

Well @click,

the problem is that the content of the textarea is user-provided content and so I am pretty sure it's a bad idea to disable automatic escaping?

My problem: My preferred wysiwyg-editor saves the text as HTML - so now there are p and br tags I have to handle - but I have to make sure that script or img are still escaped.

click's avatar

@ahoi you don't disable automatic escaping, that is where the e() function is used for in my example. What my example code does is escape everything except the tags created by the nl2br() method. This works perfectly based on your initial question.

But now that you say you do want to render certain tags and some tags not I think the safest way to go is to use HtmlPurifier. With this library you can filter html strings and only allow certain tags and or attributes (for example href, src is allowed but style or target is not allowed)

1 like

Please or to participate in this conversation.