Home > Backend Development > PHP Tutorial > PHP uses json Chinese garbled solution to explain with examples_PHP tutorial

PHP uses json Chinese garbled solution to explain with examples_PHP tutorial

WBOY
Release: 2016-07-13 17:41:19
Original
851 people have browsed it

Two commonly used functions in PHP are encode_json() and decode_json();
Let’s focus on the solution to the problem of garbled characters when the encode_json() function is used for Chinese encoding.
First, we write the array we need:
$json = array (
0 =>
array (
id => 13,
name => table tennis,
),
1 =>
array (
id => 17,
name => basketball,
)
)
?>

If we encode directly with encode_json, the output result is:
[{"id":"13","name" :null}
,{"id":"13","name":null}]
?>
Obviously, the Chinese characters are not encoded correctly. This is because json only escapes the encoding,
(assuming that our background file uses gb2312 encoding), so the above statement should convert the encoding first:
foreach ($ajax as $key=>$val)
{
$ajax[$key][name] =
urlencode($val[name]);
}
echo json_encode($json);
?>
In the same way, just decode the encoded Chinese in the client js code with decodeURI().
So far, the problem of Chinese encoding of json in php has been solved.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486150.htmlTechArticleTwo functions commonly used in php, encode_json() and decode_json(); Let’s focus on the encode_json() function. Solution to garbled characters when encoding Chinese characters. First, we write out what we need...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template