Home > Backend Development > PHP Tutorial > Method for PHP to automatically identify text encoding and convert it to the target encoding, _PHP tutorial

Method for PHP to automatically identify text encoding and convert it to the target encoding, _PHP tutorial

WBOY
Release: 2016-07-13 09:44:54
Original
883 people have browsed it

How PHP automatically identifies text encoding and converts it to the target encoding.

The example in this article describes the method of PHP automatically identifying the text encoding and converting it to the target encoding. Share it with everyone for your reference. The details are as follows:

When PHP processes pages, we use functions such as iconv or mb_convert to convert character sets, but this actually has a prerequisite. That is, we must know in advance what encoding in and out are so that we can perform the correct conversion.

Although most conversions are between gbk and utf-8, what should you do if you don’t know the encoding of the conversion object? Google has come up with such a function safeEncoding, which can easily identify the encoding of UTF8 and GBK. This function is very accurate to a certain extent, but it is not so easy to use in some more complex environments. Below I combine the differences between GBK and UTF-8 encoding and use regular expressions to determine UTF-8 Encode and use the mb_convert_encoding function to convert. In China, the most popular encodings are GBK and UTF-8, so this function automatically converts these two encodings.

/**
* @ string 需要转换的文字
* @ encoding 目标编码
**/
function detect_encoding($string,$encoding = 'gbk'){
 $is_utf8 = preg_match('%^(?:[\x09\x0A\x0D\x20-\x7E]| [\xC2-\xDF][\x80-\xBF]| \xE0[\xA0-\xBF][\x80-\xBF] | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  | \xED[\x80-\x9F][\x80-\xBF] | \xF0[\x90-\xBF][\x80-\xBF]{2} | [\xF1-\xF3][\x80-\xBF]{3} | \xF4[\x80-\x8F][\x80-\xBF]{2} )*$%xs', $string);
 if($is_utf8 && $encoding == 'utf8'){
  return $string;
 }elseif($is_utf8){
  return mb_convert_encoding($string, $encoding, "UTF-8");
 }else{
  return mb_convert_encoding($string, $encoding, 'gbk,gb2312,big5');
 }
} 

Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1044856.htmlTechArticleHow PHP automatically identifies text encoding and converts it to the target encoding. This article describes the example of PHP automatically identifying text encoding and converting it. Methods for encoding goals. Share it with everyone for your reference. Tool...
Related labels:
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