有没有办法在Ruby中解码q编码的字符串? [英] Is there a way to decode q-encoded strings in Ruby?

查看:115
本文介绍了有没有办法在Ruby中解码q编码的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  =?UTF -8?Q?J = 2E_Pablo_Fern = C3 = A1ndez?= 

有没有办法解码在Ruby中?

解决方案

我用这个来解析电子邮件主题: p>

您可以尝试以下操作:

  str ==?UTF- 8?Q?J = 2E_Pablo_Fern = C3 = A1ndez?=
如果m = / = \?([A-Za-z0-9\ - ] +)\?(B | Q) \\?([! - > @ - 〜] +)\?= / i.match(str)
case m [2]
当B#Base64编码
解码= Base64.decode64(m [3])
当Q#Q编码
decoding = m [3] .unpack(M)。first.gsub('_','')
else
p找不到关键字!!!
end
Iconv.conv('utf-8',m [1],decode)#转换为utf-8
end
/ pre>

I'm working with mails, and names and subjects sometimes come q-encoded, like this:

=?UTF-8?Q?J=2E_Pablo_Fern=C3=A1ndez?=

Is there a way to decode them in Ruby? It seems TMail should take care of it, but it's not doing it.

解决方案

I use this to parse email subjects:

You could try the following:

str = "=?UTF-8?Q?J=2E_Pablo_Fern=C3=A1ndez?="
if m = /=\?([A-Za-z0-9\-]+)\?(B|Q)\?([!->@-~]+)\?=/i.match(str)
        case m[2]
        when "B" # Base64 encoded
          decoded = Base64.decode64(m[3])
        when "Q" # Q encoded
          decoded = m[3].unpack("M").first.gsub('_',' ')
        else
          p "Could not find keyword!!!"
        end
        Iconv.conv('utf-8',m[1],decoded) # to convert to utf-8
end

这篇关于有没有办法在Ruby中解码q编码的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆