Home  >  Article  >  Backend Development  >  PHP detects whether it is utf-8 or gb2312 encoding_PHP tutorial

PHP detects whether it is utf-8 or gb2312 encoding_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:48:071010browse

There are many ways to detect string encoding in PHP. The most commonly used one is to directly use the mb_detect_encoding function, but there is a more advanced way to use the ASCII value of the character to judge.

Example 1

1. Method 1
The code is as follows
 代码如下 复制代码

function is_utf8($str)
{
$c=0; $b=0;
$bits=0;
$len=strlen($str);
for($i=0; $i<$len; $i++){
$c=ord($str[$i]);
if($c > 128){
if(($c >= 254)) return false;
elseif($c >= 252) $bits=6;
elseif($c >= 248) $bits=5;
elseif($c >= 240) $bits=4;
elseif($c >= 224) $bits=3;
elseif($c >= 192) $bits=2;
else return false;
if(($i+$bits) > $len) return false;
while($bits > 1){
$i++;
$b=ord($str[$i]);
if($b < 128 || $b > 191) return false;
$bits--;
}
}
}
return true;
}

Copy code

 代码如下 复制代码

function mb_is_utf8($string)  
{  
    return mb_detect_encoding()($string, 'UTF-8') === 'UTF-8';//新发现  

function is_utf8($str)
{
$c=0; $b=0;
$bits=0;
$len=strlen($str);
for($i=0; $i<$len; $i++){
$c=ord($str[$i]);
if($c > 128){
if(($c >= 254)) return false;
elseif($c >= 252) $bits=6;
elseif($c >= 248) $bits=5;
elseif($c >= 240) $bits=4;
elseif($c >= 224) $bits=3;
elseif($c >= 192) $bits=2;
else return false;
if(($i+$bits) > $len) return false;
while($bits > 1){
$i++;
$b=ord($str[$i]);
if($b < 128 || $b > 191) return false;
$bits--;
}
}
}
return true;
}

 代码如下 复制代码

function preg_is_utf8($string)  
{  
    return preg_match('/^.*$/u', $string) > 0;//preg_match('/^./u', $string)  

The code is as follows Copy code

2. Method 2
The code is as follows Copy code
function preg_is_utf8($string)
{
Return preg_match('/^.*$/u', $string) > 0;//preg_match('/^./u', $string)
}
http://www.bkjia.com/PHPjc/632796.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632796.htmlTechArticleThere are many ways to detect string encoding in php. The most commonly used one is to directly use the mb_detect_encoding function, but there are still A more advanced way is to use the ASCII value of the character to judge. ...
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