Link to home
Start Free TrialLog in
Avatar of shragi
shragiFlag for India

asked on

nl2br is not working in php laravel

Hi - I have the below code for text box in the webpage.
Now when ever i enter the data with line breaks and click save, the format is missing when i come back.
Example:
In the text box i entered below information in the below format
Action1 is good, but action1 is not great.

So please use the below actions2
1) action2
2) action3
3) action4
Now once I clicked save and come back tomorrow or after some time to see the comments I wrote in the text area they appear like below
Action1 is good, but action1 is not great.So please use the below actions21) action22) action33) action4

My original code is below

{!! Form::textarea('action_item_comment'.(isset($action->id) ? '['.$action->id.']' : null).'', isset($action->action_item_comment) ? $action->action_item_comment : null, array('class' => 'form-control')) !!}

Open in new window


I changed the code as below

{!! Form::textarea('action_item_comment'.(isset($action->id) ? '['.$action->id.']' : null).'', isset($action->action_item_comment) ? nl2br(e($action->action_item_comment)) : null, array('class' => 'form-control')) !!}

Open in new window

Action1 is good, but action1 is not great.<br/><br/>So please use the below actions2<br>1) action2<br>2) action3<br>3) action4
When I changed the code i got the output as below.

so how to fix this ?
The database field is set to longtext.

Thanks
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Try looking at the generated HTML document, instead of the browser output on the viewport.  You can use "view source" in most browsers to see what the server really sent to the browser, character-by-character.  My guess is that the Laravel display engine is following the recommended standards for sanitizing browser output in the interest of client safety.  This would include using htmlspecialchars() or similar.  I do not know for sure how to make Laravel stop doing this, nor would I want to!  But that said, if you're using Laravel 5 and Blade, this might be helpful, and it is certainly easy to test.

Example:
{!! $data->textarea !!}

Open in new window

Ref: https://laravel.com/docs/5.0/templates
Avatar of shragi

ASKER

Hi ray - thanks for the response, yes i am using laravel blades.
regarding view source this text area is in pop up box and when i click on view source it doesn't have code related to text area.
so i inspected the element which doesn't help much.

Dinesh
OK, I get that.  Did you try it like this, according to Displaying Raw Text with Curly Braces?
{!! $data->textarea !!}

Open in new window

Avatar of shragi

ASKER

Hi ray - if you check the code the whole form is in that format - inside {!!.......!!}
This is (probably) a string variable.  It's the textarea property in the $data object.
$data->textarea

Open in new window

In contrast, this is a bunch of nested PHP code, culminating with a function call to the textarea() static method in the Form class.
{!! Form::textarea('action_item_comment'.(isset($action->id) ? '['.$action->id.']' : null).'', isset($action->action_item_comment) ? $action->action_item_comment : null, array('class' => 'form-control')) !!}

Open in new window

My recommendation would be to try simplifying that expression, so you pass a string to Blade.
Avatar of shragi

ASKER

Hi Ray - I am not sure how to simplify this...can you share me an example with form.

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial