search

Home  >  Q&A  >  body text

ruby 如何判断一个字符串编码是 gbk 还是 utf8

ruby 如何判断一个字符串编码是 gbk 还是 utf8

高洛峰高洛峰2760 days ago766

reply all(3)I'll reply

  • PHPz

    PHPz2017-04-24 09:11:43

    UTF8 encoding and regular expressions

    reply
    0
  • PHP中文网

    PHP中文网2017-04-24 09:11:43

    Use a third-party gem library, rhardet or uchardet is recommended

    2.2.2 :001 > require 'rchardet'
     => true 
    2.2.2 :002 > CharDet.detect('测试')
     => {"encoding"=>"utf-8", "confidence"=>0.7525} 
    2.2.2 :003 > 
    

    reply
    0
  • PHP中文网

    PHP中文网2017-04-24 09:11:43

    str="abc"
    sty=str.encoding.name
    if sty=="GBK"
       puts "gbk"
    elsif sty="UTF-8"
       puts "utf8"
    else
       puts "not gbk or utf8"
    end

    reply
    0
  • Cancelreply