[PerlChina] 如何用正则确定变量的内容是utf8还是gb2312的?

agentzh agentzh at gmail.com
Wed Nov 26 22:40:46 PST 2008


2008/11/27 silent <silent2600 at gmail.com>
>
> 看了cu上的帖子:
> http://bbs.chinaunix.net/viewthread.php?tid=907172
>

我们一般使用 CPAN 上的 Encode::Guess 模块。对于长文本非常有效的,但是对于非常短的,比如两三个字的文本就不怎么准了,呵呵。

       use Encode::Guess;
       my @enc = qw( UTF-8 GB2312 Big5 GBK Latin1 );
       for my $enc (@enc) {
           my $decoder = guess_encoding($data, $enc);
           if (ref $decoder) {
               $charset = $decoder->name;
               last;
           }
       }
       if (!$charset) {
           die "Can't determine the charset of the input.\n";
       }

这里 @enc 中是尝试的 charset 数量。其实感觉用 Encode 的 decode
函数也可以直接整,只不过设一个参数让它遇到错误字节时直接抛异常即可 ;)

-agentzh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/china-pm/attachments/20081127/0e61e181/attachment.html>


More information about the China-pm mailing list