使用Blade转义字符串,呈现HTML

les*_*gar 5 php laravel blade laravel-5

我有一个$text包含一些文本和HTML代码的变量.我想渲染它的HTML,但仍然确保字符串的其余部分被转义.

$text = 'Example text with image <img src"image_1.jpg">. More text...'

// This will render the HTML but will NOT escape the string
{!! $text !!}

// This will escape and display the variable as raw string, with no HTML rendering
{{ $text }}
Run Code Online (Sandbox Code Playgroud)

在Laravel 5中,有没有办法在允许HTML的同时使用Blade转义字符串?

Mar*_*ala 0

在这种情况下,您需要对字符串进行一些手动处理。

Purifier https://github.com/mewebstudio/Purifier可能会满足您的需求。它将使字符串变得无害,然后可以安全地打印其中的 html。

否则,如果没有找到类似的东西,您需要编写自己的解析器。