Monday, 21 December 2009
For those of us who have yet to take the plunge into Ruby 1.9 it can be quite annoying to convert the encoding of a string to utf-8. I’m using couchdb so it’s quite important when bringing data in that it’s utf-8. Luckily there is a handy standard library called Iconv to help, so if you can’t guarantee the encoding of your data I recommend trying this little snippet to strip out the badness:
ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
valid_string = ic.iconv(untrusted_string + ' ')[0..-2]
blog comments powered by Disqus