Skip to content

Instantly share code, notes, and snippets.

@rantler
Last active December 29, 2015 23:29
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 rantler/7742981 to your computer and use it in GitHub Desktop.
Save rantler/7742981 to your computer and use it in GitHub Desktop.
How to *actually* replace invalid UTF-8 characters in string. :-)
a = "abc\xffdef"
a.force_encoding('UTF-8') # => "abc\xFFdef"
a.encoding.name # => "UTF-8"
# ok
a.valid_encoding? # => false
# wtf?
a.encode('UTF-8', undef: :replace, replace: '') # => "abc\xFFdef"
# wtf?
a.encode('UTF-8', 'binary', undef: :replace, replace: '') # => "abcdef"
# yay!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment