Wednesday, 21 November 2012

How to strip Specific tag from HTML using smarty?

Usually in PHP and smarty there is a function called strip_tags to strip all the tags from HTML. But in if you want to strip <img> tag alone from the HTML or any other tag from HTML content we have to use some regular expressions.

If $content is the smarty variable which holds the HTML content, then use the below regex to parse it and strip <img> tag:

{$content | regex_replace:"/(<img>|<img [^>]*>|<\\/>)/":""}

We can use this to strip any html tags. If you wanna strip <p> tag:


{$content | regex_replace:"/(<p>|<p[^>]*>|<\\/p>)/":""}

This will replace <p>, </p> and all <p many attributes> strings with an empty string.

Thats It!!

No comments:

Post a Comment