Skip to content

Instantly share code, notes, and snippets.

@cmer
Created June 13, 2011 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmer/1023038 to your computer and use it in GitHub Desktop.
Save cmer/1023038 to your computer and use it in GitHub Desktop.
# Fixes broken UTF8 strings as instructed at http://ruby.runpaint.org/encoding
def self.sanitize_string(str, request_charset=nil)
return str unless str.is_a?(String)
ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
dest = ic.iconv(str + ' ')[0..-2]
dest = encode_from_request_charset(str, request_charset) unless dest.valid_encoding?
dest
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment