A Better Solution to json_encode UTF 8 Chinese Garbled Code in php
- 2021-07-21 07:37:20
- OfStack
Recently, json_encode has been used in the interface code, and it is found on the Internet that json_encode code is set to UTF-8, so there will be no garbled code in Chinese. It has been proved that this method is really effective, but I don't know why, the code is not very useful after being used for a period of time. The following is your own solution to json_encode. There are better ways to share it!
Type 1:
This simple transcoding, urlcode before returning the required array
My code is enough.
Copy the code 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;
}
Type 2:
This is seen on the Internet, and then there is a comment that there will be an infinite loop problem, but this is obviously very comprehensive, and then I did have it after testing, and post it here for reference only
The code is as follows
/**************************************************************
*
* Use specific function Processing all elements in the array
* @param string &$array String to process
* @param string $tocode After coding
* @param string $oldcode Before coding
* @param string $function Function to execute
* @return boolean $apply_to_keys_also Is it also applied to key Upper
* @return array $array Is it also applied to key Upper
* @access public
*
*************************************************************/
function encodeOperations (&$array, $function, $tocode=false,$oldcode=false,$apply_to_keys_also = false)
{
foreach ($array as $key => $value) {
(www.ofstack.com)
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:https://www.ofstack.com/phper/31/66729.htm**********************************************************
*
* Converts an array to a JSON String (compatible with Chinese)
* @param array $array Array to convert
* @return string Converted json String
* @access public
*
*************************************************************/
function JSON($array) {
arrayRecursive($array, 'urlencode', true);
$json = json_encode($array);
return urldecode($json);
}
If the Chinese return is null empty, we need to convert characters to uft8 or use urlencode to compile the call first.
PS: Regarding json operation, here we recommend several practical json online tools for your reference:
Online JSON code verification, verification, beautification and formatting tools:
http://tools.ofstack.com/code/json
JSON Online Formatting Tool:
http://tools.ofstack.com/code/jsonformat
Online XML/JSON interconversion tool:
http://tools.ofstack.com/code/xmljson
json code online formatting/beautification/compression/editing/conversion tool:
http://tools.ofstack.com/code/jsoncodeformat
Online json compression/escape tool:
http://tools.ofstack.com/code/json_yasuo_trans
C Language Style/HTML/CSS/json Code Formatting and Beautification Tool:
http://tools.ofstack.com/code/ccode_html_css_json