1

First , please note that I'm aware that I should use {!! code here !!} to parse HTML in Laravel. But this is not the problem.

I have a textarea where people write in a description about a product they want to sell , once that saved to the database in phpmyadmin I can see that the input is of multiple lines .

However , when I do {!! $product->description !!} in my view it shows all the description in single line.

I found a workaround for it but I think there must be a better method to fix this.

the work around that I found is to do the following to replace the new lines with <br> HTML tag:

$d = $product->description;

$d = str_replace("\r\n","<br>", $d); // for Windows
$d = str_replace("\n","<br>", $d); // for Linux-like operating systems

And then I echo $d and it works just fine , is there any better approach to solve this problem ?

I am using Laravel 5.5.

2 Answers 2

2

I have a better idea for you. saving a multiple lines with \r or \n doesn't have any problem. Just use it as it is. then for show the text with line breaks use the line blow in your laravel blade.

{!! nl2br($product->description) !!}
Sign up to request clarification or add additional context in comments.

3 Comments

I'll try that and get back to you.
it works ! basically nl2br function converts the new lines to <br> tags automatically , am I right ?
Working perfectly.. Thank you.
0

you have to use "nl2br" for that The nl2br() function inserts HTML line breaks in front of each newline (\n) in a string.

<p>{!! nl2br($product->description) !!}</p>

Refer this link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.