2

我正在尝试解码我​​认为出现在 MBox 电子邮件存档中的一些引用可打印编码文本。我将举一个我遇到问题的文本的例子。

在 MBox 中,出现以下文本:

“Theresa Castel=E3o-Lawless 的划界”

正确解码,我认为这应该显示为:

“Theresa Castelão-Lawless 的划界”

我的陈述是基于它应该看起来像什么

1) 电子邮件的网络存档,其中的文本正确呈现为“Theresa Castelão-Lawless 的划界”

和 2) 这个页面,它显示“=E3”对应于引用打印https://www.ic.unicamp.br/~stolfi/EXPORT/www/ISO-8859-1-Encoding 的“ã”。 html

我已经尝试了下面的代码,但它给出了错误的输出。


string = "Demarcation by Theresa Castel=E3o-Lawless"

decoded_string = Mail::Encodings::QuotedPrintable.decode(string)

puts decoded_string + "\n"

上面代码的结果是“Theresa Castel?o-Lawless 的划界”,但如上所述,我想要“Theresa Castel?o-Lawless 的划界”

4

1 回答 1

2

当你有普通的旧红宝石来完成一项任务时,尽量避免奇怪的 Rails 东西。String#unpack是你的朋友。

"Demarcation by Theresa Castel=E3o-Lawless".
  unpack("M").first. # unpack as quoted printable
  force_encoding(Encoding::ISO_8859_1).
  encode(Encoding::UTF_8)
#⇒ "Demarcation by Theresa Castelão-Lawless"

或者,正如@Stefan 在评论中所建议的那样,可以将源编码作为第二个参数传递:

"Demarcation by Theresa Castel=E3o-Lawless".
  unpack("M").first. # unpack as quoted printable
  encode('utf-8', 'iso-8859-1')

注意: force_encoding在编码成 target 之前,需要告诉引擎这是带有欧洲口音的单字节 ISO UTF-8

于 2019-07-16T12:14:53.380 回答