PHP automatically recognizes the text encoding and translates it into a target encoding method,
In this paper, we describe the method of PHP automatic recognition of text encoding and conversion to target encoding. Share to everyone for your reference. Specific as follows:
When we are working on the page in PHP, we use the ICONV or Mb_convert functions for the conversion of the character set, but this is actually a prerequisite. That is, we have to know in advance what kind of code in and out, we can make the correct conversion.
While most conversions go between GBK and utf-8, what if you don't know what to do with the encoding of the converted object? Google comes out so a function safeencoding, can easily identify UTF8 and GBK encoding. This function is very accurate to some extent, but it is not so easy to use in some more complex environments. Here I combine the different points of GBK and UTF-8 coding, use regular expressions to determine UTF-8 encoding and use the Mb_convert_encoding function to convert. In China, basically the most popular coding is GBK and UTF-8, so this function is for these two encoding automatic conversion.
/*** @ string requires the converted text * @ encoding target 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{
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1044856.html www.bkjia.com true http://www.bkjia.com/PHPjc/1044856.html techarticle PHP Automatic recognition of text encoding and conversion to target encoding method, this article describes the PHP automatic recognition of text encoding and conversion to target encoding method. Share to everyone for your reference. ...