PHP檢測字符串是否為UTF8編碼的常用方法
更新時間:2014年11月21日 10:22:02 投稿:shichen2014
這篇文章主要介紹了PHP檢測字符串是否為UTF8編碼的常用方法,列舉了四個實例從不同的角度來實現(xiàn)這一功能,是非常實用的技巧,具有一定的學習借鑒價值,需要的朋友可以參考下
本文實例總結了PHP檢測字符串是否為UTF8編碼的常用方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
檢測字符串編碼可以有很多種方法,如利用ord獲得字符的進制然后進入判斷,或利用mb_detect_encoding函數(shù)來處理,下面整理了四種常用方法供大家參考。
例子1
復制代碼 代碼如下:
/**
* 檢測字符串是否為UTF8編碼
* @param string $str 被檢測的字符串
* @return boolean
*/
function is_utf8($str){
$len = strlen($str);
for($i = 0; $i < $len; $i++){
$c = ord($str[$i]);
if ($c > 128) {
if (($c > 247)) return false;
elseif ($c > 239) $bytes = 4;
elseif ($c > 223) $bytes = 3;
elseif ($c > 191) $bytes = 2;
else return false;
if (($i + $bytes) > $len) return false;
while ($bytes > 1) {
$i++;
$b = ord($str[$i]);
if ($b < 128 || $b > 191) return false;
$bytes--;
}
}
}
return true;
}
* 檢測字符串是否為UTF8編碼
* @param string $str 被檢測的字符串
* @return boolean
*/
function is_utf8($str){
$len = strlen($str);
for($i = 0; $i < $len; $i++){
$c = ord($str[$i]);
if ($c > 128) {
if (($c > 247)) return false;
elseif ($c > 239) $bytes = 4;
elseif ($c > 223) $bytes = 3;
elseif ($c > 191) $bytes = 2;
else return false;
if (($i + $bytes) > $len) return false;
while ($bytes > 1) {
$i++;
$b = ord($str[$i]);
if ($b < 128 || $b > 191) return false;
$bytes--;
}
}
}
return true;
}
例子2
復制代碼 代碼如下:
function is_utf8($string) {
return preg_match('%^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)*$%xs', $string);
}
return preg_match('%^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)*$%xs', $string);
}
準確率基本和mb_detect_encoding()一樣,要對一起對,要錯一起錯。
編碼檢測不可能100%準確,這個東西已經可以基本滿足要求了。
例子3
復制代碼 代碼如下:
function mb_is_utf8($string)
{
return mb_detect_encoding($string, 'UTF-8') === 'UTF-8';//新發(fā)現(xiàn)
}
{
return mb_detect_encoding($string, 'UTF-8') === 'UTF-8';//新發(fā)現(xiàn)
}
例子4
復制代碼 代碼如下:
// Returns true if $string is valid UTF-8 and false otherwise.
function is_utf8($word)
{
if (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word) == true)
{
return true;
}
else
{
return false;
}
} // function is_utf8
function is_utf8($word)
{
if (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word) == true)
{
return true;
}
else
{
return false;
}
} // function is_utf8
希望本文所述對大家的PHP程序設計有所幫助。
相關文章
PHP7安裝Redis擴展教程【Linux與Windows平臺】
這篇文章主要介紹了PHP7安裝Redis擴展的方法,簡單分析了Linux與Windows平臺Redis擴展的安裝方法,并提供了一個實例進一步分析了php操作Redis的相關技巧,需要的朋友可以參考下2016-09-09如何取得中文字符串中出現(xiàn)次數(shù)最多的子串
以下是對取得中文字符串中出現(xiàn)次數(shù)最多的子串的實現(xiàn)代碼進行了詳細的分析介紹,需要的朋友可以過來參考下2013-08-08PHP執(zhí)行Curl時報錯提示CURL ERROR: Recv failure: Connection reset by
這篇文章主要介紹了PHP執(zhí)行Curl時報錯提示CURL ERROR: Recv failure: Connection reset by peer的解決方法,需要的朋友可以參考下2014-06-06