Two methods of converting PHP array encoding GBK and UTF8 _php techniques

Source: Internet
Author: User
Tags eval parse error

I. Using Var_export (), eval () method

 * Converts the Chinese array containing GBK to Utf-8 * *
 @param array $arr   Arrays
 * @param string $in _charset The original string encoding
 * @param String $out _charset Output strings 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_export set the second argument to true , return the array prototype string, convert the string to Utf-8 encoding, and then eval perform the return (similar to an anonymous function?). ), to solve the problem perfectly.

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.

Returns if the statement is not invoked in the code string return NULL . If there is a parse error in the code, the eval() function returns FALSE.

 $a = "Hello";

 $res = eval ("return $a;");

 Print_r ($res);

 The $ designator must be escaped with a backslash

 ("\ $str = \" $str \ ";) when assigning a value;

 Echo $str;

Second, using recursion to transfer the array

 * UTF-8 encoded GBK encoding Mutual conversion/(support array)
 * *
 @param array $str   string, support array delivery
 * @param string $in _charset The original string encoding
 * @param string $out _charset Output strings 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;
 if (is_string ($STR))
  //return Iconv (' UTF-8 ', ' Gbk//ignore ', $str);
  Return mb_convert_encoding ($str, $out _charset, $in _charset);


PHP will be the array encoding GBK and utf-8 of the contents of the conversion to the basic end, the article introduced in detail, with a certain reference value, I hope this article for everyone's study and work can bring certain help.

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