Remove HTML tags from Strings on laravel blade

谁说我不能喝 提交于 2020-01-01 23:12:12

问题


I want remove HTML tags (all) from a string on laravel blade ...

code

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

output (example)

<p>hassen zouari</p>

I want that it be like this

hassen zouari

回答1:


Try to use strip_tags() function:

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

Update: Try to do something like this in a controller:

$taglessBody = strip_tags($subject->body);

Then pass this variable into a blade template and use it instead of $subject->body.




回答2:


You can use strip_tags($yourString); to strip the html tags. In blade you could achieve this by

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

hope that is helpful :)




回答3:


As for me, I use this construction:

{!! str_limit(strip_tags($post->text), $limit = 50, $end = '...') !!}

I hope, my code was helpful for somebody)




回答4:


Add the below code into your helpers

  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);
        }
    }

& use this in your blade

{{ words($sentence), $limit = 20, $end = ' ..... more') }}




回答5:


You can use

{{ strip_tags( $value->description ) }}



回答6:


just by doing this {!! $value !!} will solve your problem




回答7:


I hope, my explaination was helpful for somebody

you must know about the diffrence between {{ }} and {!! !!}

1.1) laravel have predefined you can load variable exact value by using {{$a }}

or

1.2)laravel loading with strip remove {!! $a !!}

please take it seriously this qustion answer its protect your website from cross site scripting excecution.for example if user save his name but he puted script alert.then if you using core php and not handling the strip tags then this script excution when page laod

so i give you some more harmful example 1)like i puted my name as script or in a about section and in script i write some code who get the browser cookies of open website and curl request for saving on my server.

so you understand this whats happen if i got the each user cookies by curl and i just puted a cross script in about filed and its saved in database.

for reading my research you can visit my blog http://expertsuggestion.com/.




回答8:


use this in your code

$allcms = json_decode(strip_tags(Cms::where('status','active')->get()),true);

  return response()->json(['status' => 'success','message' =>'All CMS','data' => $allcms]);


来源:https://stackoverflow.com/questions/36247382/remove-html-tags-from-strings-on-laravel-blade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!