Remove html tags from a string

HavaLite CMS

A new liteweight Content Management System (CMS)

Remove html tags from a string

The php function strip_tags() is a very usefull function, in case you wanna remove html tags from a string or just leave one or few needed tags. But what about if you wanna leave all html tags and only remove one or few tags?

For that reason i made this simple function:

function strip_html($tags, $string){
    foreach($tags as $tag) $string = preg_replace(array('/(<'.$tag.'\\b[^>]*>)|(<\/'.$tag.'>)/i'), '', $string);
    return $string;
}

// using the function 
$string = '<div class="content">This is just an example</div><span class="note">try it</span>';
echo strip_html(array('div', 'span'), $string);

comments powered by Disqus

0 Comments

    Leave a Reply

    Contact Info

    Phone: +49 941 26175

    Address: Ayman Teryaki,
    Prüfeninger Str. 48,
    93047 Regensburg, Germany

    Want more info - go to our contact page or visit Google+

    Social

    Stay up to date. Subscribe via RSS, Facebook, Twitter or Email

    Recent Comments