PHP判断一段字符串是不是utf8编码的函数


PHP #utf8 #字符串 #函数2012-05-10 21:02

这个函数也常用,哈。

01function yige_isUTF8($str){
02    $length = strlen($str);
03    for($i=0; $i<$length; $i++) {
04        $high = ord($str{$i});
05        if(($high == 0xC0) || ($high == 0xC1)) {
06            return false;
07        } elseif($high < 0x80) {
08            continue;
09        } elseif($high < 0xC0) {
10            return false;
11        } elseif($high < 0xE0) {
12            if(++$i >= $length) return true;
13            elseif(($str{$i}&"\xC0") == "\x80") continue;
14        } elseif($high < 0xF0) {
15            if(++$i >= $length) {
16                return true;
17            } elseif(($str{$i}&"\xC0") == "\x80") {
18                if(++$i >= $length) return true;
19                elseif(($str{$i}&"\xC0")=="\x80") continue;
20            }
21        }elseif($high < 0xF5) {
22            if(++$i >= $length) {
23                return true;
24            } elseif(($str{$i}&"\xC0") == "\x80") {
25                if(++$i >= $length) {
26                    return true;
27                } elseif(($str{$i}&"\xC0") == "\x80"){
28                    if(++$i >= $length) return true;
29                    elseif(($str{$i}&"\xC0") == "\x80") continue;
30                }
31            }
32        }
33        return false;
34    }
35    return true;
36}


相关文章

粤ICP备11097351号-1