English 中文(简体)
mime 主体猜测字符集( 并转换为 UTF-8)
原标题:mime body guess charset (and convert to UTF-8)

我试图解析收到的电子邮件,并希望将身体存储为数据库中的编码字符串 UTF-8 ,但我很快注意到,并非所有电子邮件都发送了 Content-Type 信头中的字符集信息。在尝试一些手动快速修补后,我决定请SO的友好人士帮忙。

老实说,我秘密地希望 String.encoding 自动返回字符串中使用的编码,然而,在我发送测试电子邮件给它之后,它总是显示 ASCII-8BIT 。 当我执行 引号可打印 时,我开始出现这个问题,如果我同时获得一些 ;charset=blabla 信息,这个选项似乎有效。

input = input.gsub(/
/, "
").unpack("M*").first
if( charset )
  return input.force_encoding(charset).encode("utf-8")
end

# This is obviously wrong as the string is not always ISO-8859-1 encoded:
return input.force_encoding("ISO-8859-1").encode("utf-8")

我尝试了互联网上发现的若干“解决方案 ”, 但大多数似乎都与文件阅读/写作有关, 也试验了几颗宝石来检测编码(尽管似乎没有人真的玩这个把戏或已经非常过时 ) 。 这应该是可能的,并且感觉答案似乎正视我的脸,希望这里的人能够对我的状况给予一些启发,并告诉我我到底做了什么错事。

  • using ruby 1.9.3
最佳回答

您可以使用“https://github.com/janx/chardet” rel=“no follow”>https://github.com/janx/chardet 来检测电子邮件文本的源编码。

示例如下:

irb(main):001:0> require  rubygems 
=> true
irb(main):002:0> require  UniversalDetector 
=> false
irb(main):003:0> p UniversalDetector::chardet( hello )
{"encoding"=>"ascii", "confidence"=>1.0}
=> nil
问题回答




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签