有些编码, 在两个函数中都为真的情况.<div>比如:<br><br>|&gt; sub is_utf8 {<br>|&gt; my $r = shift;<br>|&gt; return 1 if ($$r<br>|&gt; =~/[\x01-\x7f]|[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|<span class="Apple-style-span" style="background-color: rgb(204, 0, 0);">[\xf0-\xff][\x80-\xbf]{3}</span>/);<br>
 &nbsp; &nbsp;如果这儿,有个字的编码是[\xf0-\xff][\x80-\xbf]{3},更具体,比如是\xf0\xb1\xb1\xb1,上面红色部分匹配这个编码.那下面的gbk判断函数中的与此同时也匹配这个编码.<br>|&gt; return 0;<br>|&gt; }<br><br>|&gt; sub is_gbk {<br>|&gt; &nbsp; &nbsp; my $r = shift;<br>|&gt; &nbsp; &nbsp; return 1 if ($$r=~/[\xb0-\xf7][\xa0-\xfe]/);<br>
 &nbsp; &nbsp; &nbsp; &nbsp;\xf0<span class="Apple-style-span" style="background-color: rgb(204, 0, 0);">\xb1\xb1</span>\xb1,上行的正则也匹配这个编码中的部分.<br>|&gt; &nbsp; &nbsp; return 0;<br>|&gt; }<br><br><div>那,最后\xf0\xb1\xb1\xb1这个编码是gbk还是utf8?这就不准确了.<br><br>2008/11/27 silent &lt;<a href="mailto:silent2600@gmail.com">silent2600@gmail.com</a>&gt;<br>
&gt;<br>&gt; 了解, 可是我的那两个函数为什么不对呢?<br>&gt;<br>&gt; agentzh 写道:<br>&gt; &gt;<br>&gt; &gt; 我们一般使用 CPAN 上的 Encode::Guess 模块。对于长文本非常有效的,但是对于非常短的,比如两三个字的文本就不怎么准了,呵呵。<br>&gt; &gt;<br>&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;use Encode::Guess;<br>&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;my @enc = qw( UTF-8 GB2312 Big5 GBK Latin1 );<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;for my $enc (@enc) {<br>&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;my $decoder = guess_encoding($data, $enc);<br>&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (ref $decoder) {<br>&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$charset = $decoder-&gt;name;<br>&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;last;<br>
&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;if (!$charset) {<br>&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;die &quot;Can&#39;t determine the charset of the input.\n&quot;;<br>&gt; &gt; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&gt; &gt;<br>&gt; &gt; 这里 @enc 中是尝试的 charset 数量。其实感觉用 Encode 的 decode<br>
&gt; &gt; 函数也可以直接整,只不过设一个参数让它遇到错误字节时直接抛异常即可 ;)<br>&gt; &gt;<br>&gt; &gt; -agentzh<br>&gt; &gt;<br>&gt;<br>&gt; _______________________________________________<br>&gt; China-pm mailing list<br>&gt; <a href="mailto:China-pm@pm.org">China-pm@pm.org</a><br>
&gt; <a href="http://mail.pm.org/mailman/listinfo/china-pm">http://mail.pm.org/mailman/listinfo/china-pm</a><br><br><br>--<br>Best regards.<br>Felix New<br></div></div>