php detect character encoding code_PHP tutorial

WBOY
Release: 2016-07-13 10:54:29
Original
1033 people have browsed it

function utf8_gb2312($str, $default = 'gb2312')
{
$str = preg_replace("/[x01-x7f]+/", "", $str);
If (empty($str)) return $default;
 
$preg = array(
"gb2312" => "/^([xa1-xf7][xa0-xfe])+$/", //Regular judgment whether it is gb2312
"utf-8" => "/^[x{4e00}-x{9fa5}]+$/u", //Regular judge whether it is a Chinese character (the condition of utf8 encoding), this range is actually included Traditional Chinese characters
);

if ($default == 'gb2312') {
          $option = 'utf-8';
} else {
          $option = 'gb2312';
}

if (!preg_match($preg[$default], $str)) {
          return $option;
}
$str = @iconv($default, $option, $str);
 
//Cannot be converted to $option, indicating that the original one is not $default
If (empty($str)) {
          return $option;
}

The default encoding is gb2312, and I counted it and found that 90% of the time it is gb2312. Therefore, my detection function cannot appear to be gb2312, and the result is detected as utf8. The basic idea is:

1. Remove all ascii. If all are ascii, then it is gb2312.

2. Assume this string is gb2312, use a regular expression to check whether it is true gb2312, if not, then it is utf-8

3. Then, use iconv to convert the string into utf8. If the conversion is unsuccessful, it may not be a real gb2312 encoded character

(I have tried to be as precise as possible with regular matching, but the encoding of gb2312 is not continuous and there will still be holes), then the final encoding is utf-8.

4. Otherwise it is gb2312 encoding

After adding such a checking function, there is only one garbled code among 1000 keywords, which is much less than the nearly 100 keywords before.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632337.htmlTechArticlefunction utf8_gb2312($str, $default = 'gb2312') { $str = preg_replace(/[x01-x7f ]+/, , $str); if (empty($str)) return $default; $preg = array( gb2312 = /^([xa1-xf7][xa0-xfe])+$/, //positive. ..
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!