Pages

Sponsorship

40% off your First Order with GoDaddy.com!

Dec 2, 2009

Ruby: Strip HTML tags using Ruby


The StripHtml function strips all the HTML tags in the passed string but preserves the ones specified in the PreserveTags array.

def StripHtml(str, PreserveTags = ['a','img','p','br','i','b','u','ul','li'])
str = str.strip || ''
PreserveArr = PreserveTags.join('|') << '|\/'
str.gsub(/<(\/|\s)*[^(#{PreserveArr})][^>]*>/,'')
end
view raw naked_tags.rb hosted with ❤ by GitHub

No comments:

Post a Comment