PHP automatic recognition of text encoding and conversion to target encoding method, _php tutorial

Source: Internet
Author: User

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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.