从laravel刀片上的字符串中删除HTML标记

has*_*ari 26 laravel laravel-4 laravel-5

我想从laravel刀片上的字符串中删除HTML标签(全部)...

{!! \Illuminate\Support\Str::words($subject->body, 5,'...')  !!}
Run Code Online (Sandbox Code Playgroud)

输出(例子)

<p>hassen zouari</p>
Run Code Online (Sandbox Code Playgroud)

我希望它是这样的

hassen zouari
Run Code Online (Sandbox Code Playgroud)

Ale*_*nin 53

尝试使用strip_tags()功能:

http://php.net/manual/en/function.strip-tags.php

更新: 尝试在控制器中执行以下操作:

$taglessBody = strip_tags($subject->body);
Run Code Online (Sandbox Code Playgroud)

然后将此变量传递给刀片模板并使用它代替$subject->body.


Sah*_*dhi 12

你可以使用strip_tags($ yourString); 剥离html标签.在刀片中你可以实现这一点

{{ strip_tags($yourString) }} 
//if your string is <h1> my string </h1>
//output will be my string.
Run Code Online (Sandbox Code Playgroud)

希望有帮助:)


upe*_*dtu 8

您可以使用

{{ strip_tags( $value->description ) }}
Run Code Online (Sandbox Code Playgroud)


Pau*_*nko 7

对于我来说,我使用以下构造:

{!! str_limit(strip_tags($post->text), $limit = 50, $end = '...') !!}
Run Code Online (Sandbox Code Playgroud)

希望我的代码对某人有帮助)


Wah*_*sei 5

将以下代码添加到您的助手中

  if (! function_exists('words')) {
        /**
         * Limit the number of words in a string.
         *
         * @param  string  $value
         * @param  int     $words
         * @param  string  $end
         * @return string
         */
        function words($value, $words = 100, $end = '...')
        {
            return \Illuminate\Support\Str::words($value, $words, $end);
        }
    }
Run Code Online (Sandbox Code Playgroud)

并在你的刀片中使用它

{{ words($sentence, $limit = 20, $end = ' ..... more') }}
Run Code Online (Sandbox Code Playgroud)