Strip HTML tags using 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.

Contextual Ads

More Ruby Resources


  1. def StripHtml(str, PreserveTags = [‘a’,‘img’,‘p’,‘br’,‘i’,‘b’,‘u’,‘ul’,‘li’])
  2.         str = str.strip || ”
  3.         PreserveArr = PreserveTags.join(‘|’) << ‘|\/’
  4.         str.gsub(/<(\/|\s)*[^(#{PreserveArr})][^>]*>/,”)
  5. end

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top