Hướng dẫn strip html tags laravel

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

asked Mar 27, 2016 at 12:34

hassen zouarihassen zouari

8112 gold badges10 silver badges17 bronze badges

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.

answered Mar 27, 2016 at 12:40

Alexey MezeninAlexey Mezenin

152k23 gold badges277 silver badges263 bronze badges

1

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 <h2> my string </h2>
//output will be my string.

hope that is helpful :)

answered Mar 27, 2016 at 12:42

Sahith VibudhiSahith Vibudhi

4,5032 gold badges27 silver badges30 bronze badges

1

As for me, I use this construction:

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

I hope, my code was helpful for somebody)

answered Jun 2, 2017 at 7:34

You can use

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

linktoahref

7,3263 gold badges29 silver badges48 bronze badges

answered Feb 7, 2018 at 11:50

upendraupendra

3093 silver badges19 bronze badges

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

answered Dec 23, 2016 at 13:13

WahseiWahsei

2492 gold badges5 silver badges14 bronze badges

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.

Cody Gray

233k50 gold badges482 silver badges559 bronze badges

answered Nov 23, 2019 at 13:32

Hướng dẫn strip html tags laravel

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

answered Nov 10, 2018 at 15:48

3

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

answered Dec 19, 2019 at 4:44

Hướng dẫn strip html tags laravel

This will solve your issue

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

answered Jul 23 at 9:33

ABDOABDO

1001 silver badge6 bronze badges

PHP 8.1 and from Laravel 9, we can do this:

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

answered Sep 24 at 19:15