Created
April 18, 2011 10:00
-
-
Save mmiliaus/925098 to your computer and use it in GitHub Desktop.
Ruby Code Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Strip HTML tags | |
def strip_html(allowed = []) | |
str = self.strip || '' | |
str.gsub(/<(\/|\s)*[^(#{allowed.join('|') << '|\/'})][^>]*>/,'') | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Clean UTF-8 string | |
# | |
# -> iconvert(csv, "latin1", "utf8") | |
def iconvert(str, encoding_from, encoding_to = "utf8") | |
i = Iconv.new encoding_to, encoding_from | |
utf_str = "" | |
begin | |
utf_str << i.iconv(str) | |
rescue Exception => e | |
utf_str << e.success | |
ch, str = e.failed.split(//, 2) | |
utf_str << "?" | |
end | |
return utf_str | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment