Php encoding conversion function (the automatic conversion character set supports array conversion)

Source: Internet
Author: User
Php encoding conversion function (the automatic conversion character set supports array conversion)

  1. // The 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': $;
  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, the conversion is not performed.
  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, $ );
  20. $ FContents [$ _ key] = auto_charset ($ val, $ from, $ );
  21. If ($ key! = $ _ Key)
  22. Unset ($ fContents [$ key]);
  23. }
  24. Return $ fContents;
  25. }
  26. Else {
  27. Return $ fContents;
  28. }
  29. }

At this time, you may want to directly use iconv for transcoding. However, the iconv function requires two parameters: input encoding and output encoding, but now you do not know what encoding the accepted string is, it would be nice to get the encoding of the received characters. There are two solutions for this problem.

Solution 1When you want the client to submit data, specify the encoding submitted. in this case, you need to add a variable to specify the encoding. $ String = $ _ GET ['charset'] = 'gbk '? Iconv ('gbk', 'utf-8', $ _ GET ['str']): $ _ GET ['str']; in this case, if there is no agreement or we cannot control the client, it seems that this solution is not very useful.

Solution 2The server directly detects the received data encoding. This solution is of course the most ideal. Now the question is how to detect the encoding of a character? In php, mb_check_encoding in the mb_string extension provides the required functions. $ Str = mb_check_encoding ($ _ GET ['str'], 'gbk ')? Iconv ('gbk', 'utf-8', $ _ GET ['str']): $ _ GET ['str']; but you need to enable the mb_string extension, sometimes this extension may not be enabled on our production server. In this case, you need to use the following functions to determine the encoding.

  1. Function isGb2312 ($ string ){
  2. For ($ ID = 0; $ I 127 ){
  3. If ($ v> = 228) & ($ v <= 233 ))
  4. {
  5. If ($ I + 2) >=( strlen ($ string)-1) return true;
  6. $ V1 = ord ($ string [$ I + 1]);
  7. $ V2 = ord ($ string [$ I + 2]);
  8. If ($ v1> = 128) & ($ v1 <= 191) & ($ v2> = 128) & ($ v2 <= 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. }

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

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.