DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Html Stripping
Stripping html tags, with an optional array of tags to be preserved.
def strip_html(str, allow = ['a','img','p','br','i','b','u','ul','li'])
str = str.strip || ''
allow_arr = allow.join('|') << '|\/'
str.gsub(/<(\/|\s)*[^(#{allow_arr})][^>]*>/,'')
end






Comments
Snippets Manager replied on Thu, 2006/05/18 - 11:47pm
tags.Snippets Manager replied on Thu, 2006/05/18 - 11:47pm
tags, because they contain the letter 'p'. Her [^(#{allow_arr}] is not working the way it was probably intended to. Here is a slightly improved version.def self.strip_html(str, allow = ['a','img','p','br','i','b','u','ul','li']) str.strip! allow_arr = allow.join('\\b|') << '|/' tag_pat = %r,<(?:(?:/?)|(?:\s*))(?!#{allow_arr}).*?>, str.gsub(tag_pat, ' ') endSnippets Manager replied on Mon, 2012/05/07 - 2:13pm