Home > php教程 > php手册 > body text

PHP学习散记_编码(json_encode 中文不显示)

WBOY
Release: 2016-06-13 12:04:11
Original
1027 people have browsed it

在网上找到一种解决方法:

复制代码 代码如下:


/* 处理json_encode中文乱码 */
$data = array ('game' => '冰火国度', 'name' => '刺之灵', 'country' => '冰霜国', 'level' => 45 );
echo json_encode ( $data );
echo "
";
$newData = array ();
foreach ( $data as $key => $value ) {
$newData [$key] = urlencode ( $value );
}
echo urldecode ( json_encode ( $newData ) );
?>


后来请教了别人,还可以用base64编码,不过base64编码不可以放在URL中,百度是这样解释的:

标准的Base64并不适合直接放在URL里传输,因为URL编码器会把标准Base64中的“/”和“+”字符变为形如“%XX”的形式,而这些“%”号在存入数据库时还需要再进行转换,因为ANSI SQL中已将“%”号用作通配符。

不过我的数据是要通过POST发送的,并不在HTTP 的head中,而在message-body里,所以不受影响。

json_encode 只能接受utf-8格式的数据

urlencode base64_encode 只能接受字符串类型的参数,所以无法对整个数组进行编码。
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!