1 /**
2 * 函数 F_iconv
3 *
4 * 转换字符串编码
5 *
6 * @param string 原字符串
7 * @param string 转换编码类型[gb2312-utf8|gb2312-utf8-I|gb2312-utf8-T|utf8-gb2312|utf8-gb2312-I|utf8-gb2312-T]
8 * @return string
9 **/
10 function F_iconv($P_strig,$P_types='gb2312-utf8-i'){
11 switch(strtolower($P_types)){
12 case 'gb2312-utf8' :{ $P_strig = iconv("GB2312","UTF-8",$P_strig); break; }
13 case 'gb2312-utf8-i' :{ $P_strig = iconv("GB2312","UTF-8//IGNORE",$P_strig); break; }
14 case 'gb2312-utf8-t' :{ $P_strig = iconv("GB2312","UTF-8//TRANSLIT",$P_strig); break; }
15 case 'utf8-gb2312' :{ $P_strig = iconv("UTF-8","GB2312",$P_strig); break; }
16 case 'utf8-gb2312-i' :{ $P_strig = iconv("UTF-8","GB2312//IGNORE",$P_strig); break; }
17 case 'utf8-gb2312-t' :{ $P_strig = iconv("UTF-8","GB2312//TRANSLIT",$P_strig); break; }
18 }
19 return $P_strig;
20 }
21 /**
22 * 函数 is_utf8_encode
23 *
24 * 检测字符串是否是 utf-8
25 *
26 * @param string 被检测字符串
27 * @return boolean
28 **/
29 function is_utf8_encode($str, $flag=false){
30 static $charset = null;
31 if($charset == null){ //gbk中的汉字,但是满足utf-8的编码规范,对于这些汉字当做gbk字符处理
32 $charstr = '陇搂篓掳卤路脳脿谩猫茅锚矛铆貌贸梅霉煤眉膩膿臎墨艅艌艒奴菐菒菕菙菛菢菤菧蓱伞藝藟藠藡藱螒螔螕螖螘螙螚螛螜螝螞螠螡螢螣螤巍危韦违桅围唯惟伪尾纬未蔚味畏胃喂魏位渭谓尉慰蟺蟻蟽蟿蠀蠁蠂蠄蠅衼袗袘袙袚袛袝袞袟袠袡袣袥袦袧袨袩袪小孝校肖啸笑效楔些歇蝎鞋协挟携邪斜胁谐写械卸蟹懈泄泻谢屑薪芯锌褉褋褌褍褎褏褑褔褕褖褗褘褜褝褞褟褢';
33 $charset = str_split($charstr, 2);
34 $charset = array_flip($charset);
35 }
36 $pflag = true;$qflag = false;
37 $len = strlen($str);
38 for($i = 0; $i < $len; $i ++){ //判断是否满足utf-8的编码规范
39 $ord = ord($str[$i]);
40 if($ord < 128)continue;
41 $pflag = false;
42 if(($ord & 0xF8)==0xF0)$num = 4;
43 else if(($ord & 0xF0)==0xE0)$num = 3;
44 else if(($ord & 0xE0)==0xC0)$num = 2;
45 else return false;
46 if(!$qflag && $num==3)$qflag = true;
47 while(--$num)if(++$i==$len || (ord($str[$i]) & 0xC0)!=0x80)return false;
48 }
49 if($pflag) return $flag; //全为英文字符,默认是gbk编码的
50 if($qflag) return true; //如果该字符串中一个字符由3个字节组成,则必然是utf8编码
51 for($i = 0; $i < $len; $i++){ //对于既是utf8编码,又是gbk编码的字符进行判断
52 $ord = ord($str[$i]);
53 if($ord < 128)continue;
54 $s = $str[$i].$str[++$i];
55 if(isset($charset[$s])) return false;
56 }
57 return true;
58 }
59 $H_fname = get_param('fname'); //文件地址
60 $H_isutf = get_param('isutf'); //返回编码是否是utf8 default: 1
61 $H_isutf = $H_isutf ? true : false;
62 header("Content-Type:text/html;charset=".($H_isutf ? "utf-8" : "gb2312"));
63 $H_conte = file_get_contents($H_fname);
64 if($H_isutf) echo is_utf8_encode($H_conte) ? $H_conte : F_iconv($H_conte,'gb2312-utf8-i');
65 else echo is_utf8_encode($H_conte) ? F_iconv($H_conte,'utf8-gb2312-i') : $H_conte;