- UTF-8编码判断函数
- 发布时间:2007-10-04 22:35:59 浏览数:6360 发布者:superadmin 设置字体【大 中 小】
该函数用于判断字符串是否以UTF-8编码,若是则返回TRUE,否则返回FALSE。
以前从另一种语言(忘了是什么语言)改过一函数,但用在统计系统里发现URL来源里会有乱码,也就是部分字符会判断不准确,下面这段代码经过两周的测试还没发现什么问题function isUTF8($str){
$length=strlen($str);
for($i=0;$i<$length;$i++){
$high=ord($str{$i});
if(($high==0xC0)||($high==0xC1)){
return false;
}elseif($high<0x80){
continue;
}elseif($high<0xC0){
return false;
}elseif($high<0xE0){
if(++$i>=$length)
return true;
elseif(($str{$i}&"\xC0")=="\x80")
continue;
}elseif($high<0xF0){
if(++$i>=$length){
return true;
}elseif(($str{$i}&"\xC0")=="\x80"){
if(++$i>=$length)
return true;
elseif(($str{$i}&"\xC0")=="\x80")
continue;
}
}elseif($high<0xF5){
if(++$i>=$length){
return true;
}elseif(($str{$i}&"\xC0")=="\x80"){
if(++$i>=$length){
return true;
}elseif(($str{$i}&"\xC0")=="\x80"){
if(++$i>=$length)
return true;
elseif(($str{$i}&"\xC0")=="\x80")
continue;
}
}
}
return false;
}
return true;
}