Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect encoding

Tags:

ruby

ruby-1.9

I'm getting some string data from the web, and I suspect that it's not always what it says it is. I don't know where the problem is, and I just don't care any more. From day one on this project I've been fighting Ruby string encoding. I really want some way to say: "Here's a string. What is it?", and then use that data to get it to UTF-8 so that it doesn't explode gsub() 2,000 lines down in the depths of my app. I've checked out rchardet, but even though it supposedly works for 1.9 now, it just blows up given any input with multiple bytes... which is not helpful.

like image 540
Phil Kulak Avatar asked Jun 19 '10 05:06

Phil Kulak


2 Answers

why not try use https://github.com/brianmario/charlock_holmes to get the exact encoding. Then also use it to convert to UTF8

    require 'charlock_holmes'
    class EncodeParser
      def initialize(text)
        @text = text
      end

      def detected_encoding
        CharlockHolmes::EncodingDetector.detect(@text)[:encoding]
      end

      def convert_to_utf8
        CharlockHolmes::Converter.convert(@text, detected_encoding, "UTF-8")
      end
    end

then just use EncodeParser.new(text).detected_encoding or EncodeParser.new(text). convert_to_utf8

like image 126
Olalekan Sogunle Avatar answered Oct 29 '22 03:10

Olalekan Sogunle


It is impossible to tell from a string what encoding it is in. You always need some additional metadata that tells you what the string's encoding is.

If you get the string from the web, that metadata is in the HTTP headers. If the HTTP headers are wrong, there is absolutely nothing that you or Ruby or anyone else can do. You need to file a bug with the webmaster of the site where you got the string from and wait till he fixes it. If you have a Service Level Agreement with the website, file a bug, wait a week, then sue them.

like image 27
Jörg W Mittag Avatar answered Oct 29 '22 02:10

Jörg W Mittag