PHP Encoding conversion function (automatic conversion character set supports array conversion)

Source: Internet
Author: User
  1. Automatic conversion character set supports array conversion
  2. function Auto_charset ($fContents, $from = ' GBK ', $to = ' utf-8 ') {
  3. $from = Strtoupper ($from) = = ' UTF8 '? ' Utf-8 ': $from;
  4. $to = Strtoupper ($to) = = ' UTF8 '? ' Utf-8 ': $to;
  5. if (Strtoupper ($from) = = = Strtoupper ($to) | | empty ($fContents) | | (Is_scalar ($fContents) &&!is_string ($fContents))) {
  6. If the encoding is the same or the non-string scalar is not converted
  7. return $fContents;
  8. }
  9. if (is_string ($fContents)) {
  10. if (function_exists (' mb_convert_encoding ')) {
  11. Return mb_convert_encoding ($fContents, $to, $from);
  12. } elseif (Function_exists (' Iconv ')) {
  13. Return Iconv ($from, $to, $fContents);
  14. } else {
  15. return $fContents;
  16. }
  17. } elseif (Is_array ($fContents)) {
  18. foreach ($fContents as $key = = $val) {
  19. $_key = Auto_charset ($key, $from, $to);
  20. $fContents [$_key] = Auto_charset ($val, $from, $to);
  21. if ($key! = $_key)
  22. Unset ($fContents [$key]);
  23. }
  24. return $fContents;
  25. }
  26. else {
  27. return $fContents;
  28. }
  29. }
Copy Code

At this point, you may think of using iconv directly to transcode, but iconv this function needs to provide two parameters for the input encoding and output encoding, and now do not know what the accepted string is what encoding, if you can get the receiver character is what encoding is good. For this problem, there are two kinds of options for reference.

Scenario One to specify the encoding to commit when the client submits the data, it is necessary to give more than one variable to specify the encoding. $string = $_get[' charset '] = = = ' GBK '? Iconv (' GBK ', ' utf-8 ', $_get[' str '): $_get[' str '); In this case, it seems that this scenario is not working well if there is no agreement or we cannot control the client.

Scenario Two detects the received data encoding directly from the server side. This scheme is certainly the most ideal, and now the question is how to detect the encoding of a character? In this case, in PHP, Mb_string's mb_check_encoding in this extension provides the functionality we need. $str = mb_check_encoding ($_get[' str '), ' GBK ')? Iconv (' GBK ', ' utf-8 ', $_get[' str '): $_get[' str '); but this needs to be opened mb_string this extension, and sometimes our production server does not have this extension open. In this case, you need to use the following function to determine the encoding.

  1. function isGb2312 ($string) {
  2. for ($i =0; $i 127) {
  3. if (($v >= 228) && ($v < = 233))
  4. {
  5. if ($i +2) >= (strlen ($string)-1)) return true;
  6. $v 1 = ord ($string [$i +1]);
  7. $v 2 = Ord ($string [$i +2]);
  8. if (($v 1 >=) && ($v 1 < =191) && ($v 2 >=128) && ($v 2 < = 191))
  9. return false;
  10. Else
  11. return true;
  12. }
  13. }
  14. }
  15. return true;
  16. }
  17. function IsUtf8 ($string) {
  18. Return Preg_match ('%^ (?:
  19. [\x09\x0a\x0d\x20-\x7e] # ASCII
  20. | [\XC2-\XDF] [\X80-\XBF] # Non-overlong 2-byte
  21. | \XE0[\XA0-\XBF][\X80-\XBF] # excluding overlongs
  22. | [\xe1-\xec\xee\xef] [\X80-\XBF] {2} # straight 3-byte
  23. | \XED[\X80-\X9F][\X80-\XBF] # excluding surrogates
  24. | \XF0[\X90-\XBF][\X80-\XBF]{2} # Planes 1-3
  25. | [\xf1-\xf3] [\X80-\XBF] {3} # planes 4-15
  26. | \XF4[\X80-\X8F][\X80-\XBF]{2} # Plane 16
  27. ) *$%xs ', $string);
  28. }
Copy Code

Here we can use any of these functions to implement the encoding detection and convert it to the specified encoding. $str = isGb2312 ($_get[' str '), ' GBK ')? Iconv (' GBK ', ' utf-8 ', $_get[' str '): $_get[' str '];

  • Related Article

    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.