Blade: escaping text and allowing new lines

PhpLaravelEscapingBlade

Php Problem Overview


I have a user-input text displayed on one of the pages. I want to allow new lines, though. How do I display the text, so it is escaped AND allows new lines?

I used nl2br() and Blade's tripple brackets {{{$text}}}, however, obviously, the tripple brackets escape <br/> tags as well.

Is there a way to combine escaping and HTML's new lines using Blade?

Thanks.

Php Solutions


Solution 1 - Php

You can do the escaping first, using e() and then apply nl2br():

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

e() is the function Blade uses when compiling triple brackets

Solution 2 - Php

You can use this

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

Solution 3 - Php

in simple approach you can use it in blade File > {!! nl2br(e($onlineSetting->instruction)) !!}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlesssugarView Question on Stackoverflow
Solution 1 - PhplukasgeiterView Answer on Stackoverflow
Solution 2 - PhpSalarView Answer on Stackoverflow
Solution 3 - Phppankaj kumarView Answer on Stackoverflow