Ryan Arief
Ryan Arief

Reputation: 1055

substr doesn't work after using strip_tags

I want to remove all tags before showing them on preview mode (just some text).

I have this code:

$text = strip_tags($item['content']);
echo substr($text,0,13); 

here is my $item['content'] is something like this

 <div class="note note-success">
                                    <p>
                                         Font Awesome gives you scalable
 vector icons that can instantly be customized — size, color, drop 
shadow, and anything that can be done with the power of CSS. The 
complete set of 439 icons in Font Awesome 4.1.0
                                    </p>
                                     For more info check out: <a target="_blank" href="http://fortawesome.github.io/Font-Awesome/icons/">http://fortawesome.github.io/Font-Awesome/icons/</a>
                                </div>

The problem is that when I use substr it doesn't show anything, but when I use normal echo, it shows the content of the variable that was stripped before.

Does strip_tags not give string output?

Upvotes: 1

Views: 1240

Answers (2)

Ashish Patel
Ashish Patel

Reputation: 1041

strip_tags() function works only when following type of html text. what you are doing is convert html encoded text so, it will not be parse.

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);

For your example you can use like this:

$text = htmlentities($item['content']);
echo substr(html_entity_decode($text),0,13); or
echo substr($text,0,13);

Upvotes: 1

Minzkraut
Minzkraut

Reputation: 2187

Try to remove whitespaces before outputting your substring:
$new = str_replace(' ','',$text); (Use trim instead as @mario.klump said)

$text = strip_tags($item['content']);
$new = trim($text);
echo substr($new,0,13);

Upvotes: 2

Related Questions