Is there a way to decode q-encoded strings in Ruby?

前端 未结 6 610
星月不相逢
星月不相逢 2021-01-13 16:05

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 t

6条回答
  •  礼貌的吻别
    2021-01-13 16:44

    Decoding on a line-per-line basis:

    line.unpack("M")
    

    Convert STDIN or file provided input of encoded strings into a decoded output:

    if ARGV[0]
      lines = File.read(ARGV[0]).lines
    else
      lines = STDIN.each_line.to_a
    end
    
    puts lines.map { |c| c.unpack("M") }.join
    

提交回复
热议问题