Php json_encodeUTF-8 Chinese garbled problem, _ PHP Tutorial

Source: Internet
Author: User
Php json_encodeUTF-8 Chinese garbled problem ,. Php json_encodeUTF-8 Chinese garbled problem, recently used in the interface code json_encode, found on the Internet said json_encode encoding set to UTF-8 Chinese will not garbled, it is verified that the json_encode UTF-8 in php is garbled in Chinese,

Recently used in the interface code json_encode, on the Internet to find that json_encode is set to UTF-8 Chinese will not garbled, verified this method is indeed effective, but do not know why, the code won't work after a while. The following is your solution to json_encode. There are better ways to share it!

First:

This simple code conversion, urlcode and then return the required array

My code is enough.

The code is as follows:
Public static function encodeOperations ($ array)

{

Foreach (array) $ array as $ key => $ value ){

If (is_array ($ value )){

EncodeOperations ($ array [$ key]);

} Else {

$ Array [$ key] = urlencode (mb_convert_encoding ($ value, 'utf-8', 'gbk '));

}

}

Return $ array;

}

Second:

This is seen on the Internet, and then there is a comment saying that there will be an infinite loop problem, but this is obviously very comprehensive, and I did have it after testing, posted here for reference only

The code is as follows:

/*************************************** ***********************

*

* Use a specific function to process all elements in the array

* @ Param string & $ array the string to be processed

* @ Param string $ After tocode encoding

* @ Param string $ Before oldcode encoding

* @ Param string $ function the function to be executed

* @ Return boolean $ apply_to_keys_also whether it is also applied to the key

* @ Return array $ whether array is also applied to the key

* @ Access public

*

**************************************** *********************/

Function encodeOperations (& $ array, $ function, $ tocode = false, $ oldcode = false, $ apply_to_keys_also = false)

{

Foreach ($ array as $ key => $ value ){
(Www.111cn.net)
If (is_array ($ value )){

EncodeOperations ($ array [$ key], $ function, $ apply_to_keys_also );

} Else {

If ($ tocode & $ oldcode ){

If (function_exists (mb_convert_encoding )){

$ Value = mb_convert_encoding ($ value, $ tocode, $ oldcode );

} Else {

Return "error ";

}

}

$ Array [$ key] = $ function ($ value );

}

If ($ apply_to_keys_also & is_string ($ key )){

$ New_key = $ function ($ key );

If ($ new_key! = $ Key ){

$ Array [$ new_key] = $ array [$ key];

Unset ($ array [$ key]);

}

}

}

Return $ array;

}

/***** From: Begin **********************************************************

*

* Convert an array to a JSON string (compatible with Chinese characters)

* @ Param array $ array the array to be converted

* @ Return string the converted json string

* @ Access public

*

**************************************** *********************/

Function JSON ($ array ){

ArrayRecursive ($ array, 'urlencode', true );

$ Json = json_encode ($ array );

Return urldecode ($ json );

}

If the return value for Chinese characters is null, convert the characters to uft8 or use urlencode to compile the call.





Zookeeper UTF-8 Chinese garbled problem, recently used in the interface code json_encode, found on the Internet said json_encode encoding set to UTF-8 Chinese will not garbled, verified this office...

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.