Home >Backend Development >PHP Tutorial >PHP判断字符串编码是否为utf8的函数

PHP判断字符串编码是否为utf8的函数

WBOY
WBOYOriginal
2016-06-20 13:04:591104browse

无意中找到这么一段PHP判断字符串编码是否为utf8的函数,这个应该会用到的,保存下来存个档,这个是据称完美的判断函数,希望是完美的!

// 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

 


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn