phphtmllaravelhtml-tablenl2br

In table, <br /> tags displaying on the screen rathen than new line


I have a <table> and inside <td> tags inside it where I am displaying the retrieved data from the backend. Before saving my data to database, I used this to convert \n and \r into <br /> tags:

$data = nl2br($request->data);
$data = str_replace(array("\r\n", "\n\r", "\r", "\n"), "", $data);

To retrieve data, I tried directly echo-ing the value - ({{ }} is echo for Laravel)

<tr> 
  <td>{{ $item->data }}</td>
</tr>

However, this is displaying <br /> tags on the page instead of turning them into new line breaks.


Then I tried using this approach, which I believe would work, however, this is also displaying the &#13; on the text rather than converting it to new lines.

<tr> 
  <td>@if ($item){{ str_replace('<br />', '&#13;', $item->data) }}@endif</td>
</tr>

Also tried replacing br tags to \n with using '\n' and '\\n' inside str_replace() but no luck

How can I turn <br /> tags into new lines as usual inside table?


Solution

  • This has nothing to do with the table.

    See the documentation:

    Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks.

    and

    By default, Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:

    Hello, {!! $name !!}.
    

    Be very careful when echoing content that is supplied by users of your application. Always use the escaped, double curly brace syntax to prevent XSS attacks when displaying user supplied data.