PHP檢測字串是否為UTF8編碼的常用方法

2022-09-27 03:54:10 字數 1264 閱讀 4165

例子1複製** **如下:

/*** 檢測字元程式設計客棧串是否為utf8編碼

* @param string $str 被檢測的字串

* @return boolean

*/function is_utf8($str)}}

return true;

}例子2

複製** **如下:

function is_utf8($string)      # straight 3-byte

&ndjiwlicbsp;   |     \xed[\x80-\x9f][\x80-\xbf]             # excluding surrogates

|     \xf0[\x90-\xbf][\x80-\xbf]     # planes 1-3

| [\xf1-\xf3][\x80-\xbf]             # planes 4-15

|     \xf4[\x80-\x8f][\x80-\xbf]     # plane 16

)*$%xs', $string);     

}準確率基本和mb_detect_encoding()一樣,要對一起對,要錯一起錯。

編碼檢測不可能100%準確,這個東西已經可以基本滿足要求了。

例子3複製** **如下:

function mb_is_utf8($string)  

例子4複製** **如下:

// returns true if $string is valid utf-8 and false otherwise.  

function is_utf8($word)  

[".chr(128)."-".chr(191)."][".chr(128)."-".chr(191)."])/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."][".chr(128)."-".chr(191)."][".chr(128)."-".chr(191)."])$/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."][".chr(128)."-".chr(191)."][".chr(128)."-".chr(191)."])/",$word) == true)  

else  

} // function is_utf8

本文標題: php檢測字串是否為utf8編碼的常用方法

本文位址: /wangluo/php/116294.html

檢測位元組流是否是UTF8編碼

幾天前偶爾看到有人發帖子問 如何自動識別判斷url中的中文引數是gb2312還是utf 8編碼 也拜讀了wcwtitxu使用巨牛的正規表示式檢測utf8編碼的演算法。使用無數或條件的正規表示式用起來卻是效能不高。先聊聊原理 utf8的編碼規則如下表 看起來很複雜,總結起來如下 ascii碼 u 00...

檢測位元組流是否是UTF8編碼

utf8的編碼規則總結起來如下 ascii碼 u 0000 u 007f 不編碼 其餘編碼規則為 第乙個byte二進位制以形式為n個1緊跟個0 n 2 0後面的位數用來儲存真正的字元編碼,n的個數說明了這個多byte位元組組位元組數 包括第乙個byte 接下來會有n個以10開頭的byte,後6個bi...

Python 遞迴 檢測字串是否為 回文

源 檢測字串是否是 回文字串 回文概念 從前往後念 與 從後往前念 都一樣。def hui wen str 基線條件 如果字串的長度小於 2 位,那麼改字串就為 回文字串 if len str 2 是 回文 return true 如果字串的 第一位 與 最後一位 不相等,那麼不為 回文字串 eli...