61

I want to 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 it be like this

hassen zouari
1
  • you can use strip_tags($string) to strip the html tags. Commented Mar 27, 2016 at 12:38

12 Answers 12

114

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks :) but when i add {!! \Illuminate\Support\Str::words($subject->body, 5,'...') !!} as the string that i want ahnge to this function it shows an error ..
32

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 :)

1 Comment

thanks :) but when i add {!! \Illuminate\Support\Str::words($subject->body, 5,'...') !!} as the string that i want ahnge to this function it shows an error ..
12

As for me, I use this construction:

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

I hope, my code was helpful for somebody)

1 Comment

str_limit doesn't exist now, you can use facade to achieve this functionality. {!! Str::limit(strip_tags($post->description), $limit = 50, $end = '...') !!}
8

You can use

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

Comments

5

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

Comments

4

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.

Comments

3

This will solve your issue

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

1 Comment

this worked for me on laravel 8
1

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

3 Comments

but you can not apply limit after using this tag.
Did you mean limiting the characters?
It will not remove the tags but will apply the HTML on the text. strip_tags will actually remove HTML without applying it.
1

when we used strip_tags, it will not convert the whitespace into space, it will display as HTML like &nbsp!; this. How to remove this and the tags too?

{!! Str::limit(strip_tags($subject->body), $limit = 50, $end = '...') !!}

Comments

0

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

Comments

0

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

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

Comments

0

Keep htmlentities, line break, ...

Long code, but that satisfies me in all situations

@php
$txt = nl2br(Str::words(strip_tags($var->txt), 30, '...'));
$txt = preg_replace('/(<br\s*\/?>\s*){2,}/', '<br />', $txt);
@endphp
{!! $txt !!}

Explications

  1. Remove HTML tags
  2. Reduice string to 30 words
  3. Replace new lines with BR tags
  4. Remove contiguous BR tags

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.