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