PHP array encoding: Two Methods for mutual conversion between gbk and utf8:

Source: Internet
Author: User

PHP array encoding: Two Methods for mutual conversion between gbk and utf8:

1. Use the var_export () and eval () Methods

/*** Convert a Chinese array containing GBK into UTF-8 ** @ param array $ arr array * @ param string $ in_charset original string encoding * @ param string $ out_charset output string encoding * @ return array */function array_iconv ($ arr, $ in_charset = "gbk", $ out_charset = "UTF-8") {$ ret = eval ('Return '. iconv ($ in_charset, $ out_charset, var_export ($ arr, true ). ';'); return $ ret; // After transcoding, You can output json // return json_encode ($ ret );}

The principle is simple.var_exportSet the second parametertrue, Returns the array prototype string, converts the string to UTF-8 encoding, and then useseval(Like Anonymous functions ?), This is a perfect solution.

Eval () function summary:

Condition:eval() The function calculates the string according to the PHP code. The string must be a valid PHP code and must end with a semicolon.

If it is not called in the code stringreturn Statement, then returnNULL. If a parsing error exists in the codeeval() The function returns false.

$ A = "hello"; $ res = eval ("return $ a;"); print_r ($ res ); // when assigning values, you must use a backslash to escape the $ identifier eval ("\ $ str = \" $ str \ ";"); echo $ str;

Ii. use recursion to transcode the Array

/*** UTF-8 encoding GBK encoding mutual conversion/(array supported) *** @ param array $ str string, * @ param string $ in_charset original string encoding * @ param string $ out_charset output string encoding * @ return array */function array_iconv ($ str, $ in_charset = "gbk", $ out_charset = "UTF-8") {if (is_array ($ str) {foreach ($ str as $ k => $ v) {$ str [$ k] = array_iconv ($ v);} return $ str;} else {if (is_string ($ str )) {// return iconv ('utf-8', 'gbk // IGNORE ', $ str); return mb_convert_encoding ($ str, $ out_charset, $ in_charset );} else {return $ str ;}}}

Summary

PHP converts the array encoding gbk and UTF-8 to each other. This is basically the end of the article, which is very detailed and has reference value, I hope this article will help you in your study and work.

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.