越简单越好!

UTF-8编码判断函数

发表于 2007-10-16 14:40 | 1146次阅读 0次点赞   PHP

该函数用于判断字符串是否以UTF-8编码,若是则返回TRUE,否则返回FALSE。

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;
}

返回顶部 ^