首頁 php教程 php手册 PHP检测字符串是否为UTF8编码4种方法

PHP检测字符串是否为UTF8编码4种方法

May 26, 2016 am 08:20 AM

检测字符串编码可以有很多种方法,如利用ord获得字符的进制然后进入判断,或利用mb_detect_encoding函数来处理,下面整理了几种方法.

例子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; 
}
登入後複製

例子2,代码如下:

function is_utf8($string) {  
    return preg_match(&#39;%^(?:  
            [\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&#39;, $string);
登入後複製

准确率基本和mb_detect_encoding()一样,要对一起对,要错一起错,编码检测不可能100%准确,这个东西已经可以基本满足要求了.

例子3,代码如下:

function mb_is_utf8($string)    
{    
    return mb_detect_encoding($string, &#39;UTF-8&#39;) === &#39;UTF-8&#39;;//新发现    
}
登入後複製

例子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
登入後複製


教程链接:

随意转载~但请保留教程地址★

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1317
25
PHP教程
1268
29
C# 教程
1246
24