Created
October 7, 2013 14:38
-
-
Save dylancashman/6869084 to your computer and use it in GitHub Desktop.
Utf8Cleaner for encoding problems.
This file contains hidden or 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
module Utf8Cleaner | |
def to_utf8(new_value) | |
if new_value.is_a? String | |
begin | |
# Try it as UTF-8 directly | |
new_value.force_encoding('UTF-8') | |
unless new_value.valid_encoding? | |
# Some of it might be old Windows code page | |
new_value.encode!( 'UTF-8', 'Windows-1252' ) | |
end | |
rescue EncodingError | |
# Force it to UTF-8, throwing out invalid bits | |
new_value.encode!( 'UTF-8', invalid: :replace, undef: :replace ) | |
end | |
end | |
new_value | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment