R00t
R00t

Reputation: 186

Remove text formatting tags

I want to remove all formatting tags of from a string (to show a kinda of summary). To achieve this, I used the str_replace method from PHP. here is my code :

<?php $tags_to_delete = array('<p>', '<h?>', '<b>', '<i>', '<u>', '<strike>', '<code>','<blockquote>','<font color="#??????">','<font color="#???">', '<font size="?">', '<center>', '<em>', '<font face="?">', '<strong>', '</p>', '</h?>', '</b>', '</i>', '</u>', '</strike>', '</code>','</blockquote>','</font>', '</center>', '</em>', '</strong>');?>

$article->description = str_replace($tags_to_delete, "", $article->description);
$article->description=substr($article->description,0,150).'...'; 
echo $article->description; 
die(); 

It works great for all tags except the <font color="#??????"> and <font face="?"> . In other programming language, I've always used the ? symbol to select any value, it could be 'a' or '2', the ? character should represent anything. Is this different in PHP ? If someone can help me with this, I'd be really thankful !

Upvotes: 0

Views: 621

Answers (1)

jeroen
jeroen

Reputation: 91734

You can use strip_tags and just specify the tags you want to keep, for example:

$article->description = strip_tags($article->description, '<a><img>');

Upvotes: 1

Related Questions