Code Snippet > PHP

A Better Strip HTML Tags Function for PHP

strip_tags is a default HTML Tag stripper in PHP. However, it can't strip some of the tags, so this enhanced version called strip_html_tags will remove more html elements.

PHP

function strip_html_tags( $text )
{
    $text = preg_replace(
        array(
          // Remove invisible content
            '@<head[^>]*?>.*?</head>@siu',
            '@<style[^>]*?>.*?</style>@siu',
            '@<script[^>]*?.*?</script>@siu',
            '@<object[^>]*?.*?</object>@siu',
            '@<embed[^>]*?.*?</embed>@siu',
            '@<applet[^>]*?.*?</applet>@siu',
            '@<noframes[^>]*?.*?</noframes>@siu',
            '@<noscript[^>]*?.*?</noscript>@siu',
            '@<noembed[^>]*?.*?</noembed>@siu',
          // Add line breaks before and after blocks
            '@</?((address)|(blockquote)|(center)|(del))@iu',
            '@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
            '@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
            '@</?((table)|(th)|(td)|(caption))@iu',
            '@</?((form)|(button)|(fieldset)|(legend)|(input))@iu',
            '@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
            '@</?((frameset)|(frame)|(iframe))@iu',
        ),
        array(
            ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',"$0", "$0", "$0", "$0", "$0", "$0","$0", "$0",), $text );
 
    // you can exclude some html tags here, in this case B and A tags        
    return strip_tags( $text , '<b><a>' );
}

Source: http://nadeausoftware.com/articles/2007/09/php_tip_how_strip_html_tags_web_page



Show Some Love, Spread This Post!

9 comments

Amit Parashar Thu, 26th April 2012 any reason I get errors related to memory size?
Reply
Nayan Jha Wed, 14th March 2012 Great script. I really needed this!
Its not only great its awesome..
Thanks a lot
Reply
Max Sat, 10th March 2012 No.
If you want to allow some tags never ever use strip_tags exclude. When allowing any HTML you have entered the black zone and need to think about a well formed filter like HTMLPURIFY.
Protecting yourself against server side attack is much more important than worrying about output that will upset your vanity.
Reply
hakkatil Tue, 21st February 2012 great script, thank you
Reply
KCharle Thu, 16th February 2012 Im getting a Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 3435 bytes) in /var/www/purifier/test.php on line 43 which is " ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',"$0", "$0", "$0", "$0", "$0", "$0","$0", "$0",), $text ); ". Any suggestions?
Reply
Bo Allen Sun, 12th February 2012 Broken. Line 28 should be "strip_tags", not "strip_html_tags".
Reply
Kevin Liew Sun, 12th February 2012 Thanks! I have updated the script.
Hemant Jadhav Tue, 4th October 2011 That's awesome script... thanks
Reply
Willie Matthews Thu, 4th August 2011 Great script. I really needed this!
Reply

Leave a comment

Have something to say? Drop a comment! No HTML tags are allowed in the comment textfield.

Advertisement