PHP语言: Codee#1934
01
02
03 //探测编码。注意:由于gb2312,gbk,big5范围有重合,按照最小范围判断
04 function get_encoding($string)
05 {
06 $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
07 $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
08 $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
09 $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
10
11 foreach( $re as $encoding=>$pattern )
12 {
13 $rstring = preg_replace($pattern, "", $string);
14 if( $rstring == "")
15 {
16 return $encoding;
17 }
18 }
19 return null;
20 }
21
22 ?>
02
03 //探测编码。注意:由于gb2312,gbk,big5范围有重合,按照最小范围判断
04 function get_encoding($string)
05 {
06 $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
07 $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
08 $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
09 $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
10
11 foreach( $re as $encoding=>$pattern )
12 {
13 $rstring = preg_replace($pattern, "", $string);
14 if( $rstring == "")
15 {
16 return $encoding;
17 }
18 }
19 return null;
20 }
21
22 ?>
No comments:
Post a Comment