站长论坛
标题:
UTF-8编码判断函数
[打印本页]
作者:
superadmin
时间:
2007-10-4 22:35
标题:
UTF-8编码判断函数
该函数用于判断字符串是否以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;
}
复制代码
欢迎光临 站长论坛 (http://tzlink.com/bbs/)
Powered by Discuz! X3.2