2

I'm following the excellent laravel guide on Laracasts and have gotten to the point where we're outputting song titles from a mysql database in video 7.

I wanted to go ahead and add the lyrics which I've done just fine.

Now I'd just like to format it a little better, to add new lines where required.

I've attempted to use str_replace in my view in this manner;

<p>{{str_replace("\r","\n", $song->lyrics)}}</p>

But nothing appears to happen. If I use
instead of \n, I can see that the carriage return is detected in the correct places, but my output appears like this;

looking oh so pretty,<br> I've just got to find my way.<br>

Where the < br>'s are just output as normal text.

Can anyone tell me if I'm approaching this in the right way? I've tried double and triple curly braces and I think I may be forgetting something that mentioned an exclamation mark, but I can't seem to find where it was mentioned again.

Any help would be appreciated.

chinrub
  • 55
  • 2
  • 6
  • use `{!! nl2br(e($text)) !!}` it is easier and more effective: or make your own echo format , both are mentioned here: http://stackoverflow.com/questions/28569955/how-do-i-use-nl2br-in-laravel-5-blade?noredirect=1&lq=1 – Majed DH Jan 18 '17 at 01:28

1 Answers1

5

In Laravel 5 you should use {!! !!} to output variable without escaping:

{!! str_replace("\r","\n", $song->lyrics) !!} 

Read more: http://laravel.com/docs/master/upgrade#upgrade-5.0 (Blade Tag Changes section)

Limon Monte
  • 52,539
  • 45
  • 182
  • 213
  • Thanks alot @limonte! I knew I saw something about exclamation marks somewhere. I went with this in the end

    {!! str_replace("\r","
    ", $song->lyrics) !!}

    Leads me to another question where I'd like to know why \n doesn't add a new line where
    does. It's not important though as I do have something that works for me
    – chinrub May 12 '15 at 10:46
  • 2
    This worked for me `{!! str_replace("\n","
    ", $song->lyrics) !!}` . Thank you
    – w3spi Dec 02 '15 at 21:31