Table of Contents
一、通过json_encode第二个参数来解决
二、通过url编码解决中文问题
总结
Home Backend Development PHP Tutorial PHP--json_encode中文编码问题

PHP--json_encode中文编码问题

Jun 23, 2016 pm 01:03 PM

版权声明:此文章转载自迹忆( http://www.onmpw.com/tm/xwzj/prolan_122.html?

hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io )

如需转载请联系听云College团队成员小尹 邮箱:yinhy#tingyun.com

在PHP项目中会经常遇到中文乱码,这是一个比较恼人的问题。不过,当需要将内容输出到网页上的时候,我们遵照以下两个原则一般情况下是不会出现中文乱码的。

第一就是在html头部添加

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Copy after login

第二点就是保证文件的编码和meta设置的编码一致。也就是说,如果我们第一条设置的charset为UTF-8,那我们的文件编码也要设置成UTF-8。二者保持一致即可。

当然上面是针对于网页的情况,但是现在随着Ajax在web应用中占得比重越来越大,json格式的数据在数据传输中应用也越来越广。因此在PHP中使用json_encode对数据进行json转换的时候也会遇到中文乱码或者是对中文进行编码的问题。

举个例子来说

$data = array('id'=>1,'name'=>'迹忆博客','parId'=>0);echo json_encode($data);
Copy after login

上面代码的输出结果为

{"id":1,"name":"\u8ff9\u5fc6\u535a\u5ba2","parId":0}
Copy after login

我们看,函数对中文’迹忆博客’进行了16进制转码。这不能说是错误,因为16进制表示的就是正确的中文,我们拿到这个结果以后,在前端通过js同样也可以得到我们想要的结果。但是,这样使用起来还是比较麻烦的。当然情况也会根据PHP的版本的不同而有所变化。

对于PHP的一些版本其结果会是如下的情况

{"id":1,"name":null,"parId":0}
Copy after login

中文无法被正确的解析出来。

没有办法,中文问题就是这么麻烦。谁让计算机不是中国人发明的呢。当然这些是有解决方法的,下面我们就来了解两个解决的办法。

一、通过json_encode第二个参数来解决

针对上面问题,在PHP>=5.4.0 的版本已经可以直接通过json_encode函数本身来解决。那就是第二个参数加上JSON_UNESCAPED_UNICODE。

$data = array('id'=>1,'name'=>'迹忆博客','parId'=>0);echo json_encode($data, JSON_UNESCAPED_UNICODE);
Copy after login

现在就能得到正确的结果

{"id":1,"name":"迹忆博客","parId":0}
Copy after login
Copy after login

结果是能正确的得到了。但是,对于PHP的版本总不能让大家都换成5.4及以上的版本吧。那对于5.4以下的版本应该怎么处理呢?

二、通过url编码解决中文问题

我们知道,在PHP中有两个函数 urlencode 和 urldecode。我们可以通过urlencode函数将中文进行url编码,这样在字符串中就不会再有中文,也就不会遇到中文编码的问题。

$str = “迹忆博客”;echo urlencode($str);
Copy after login

对迹忆博客进行url编码,其结果如下

%E8%BF%B9%E5%BF%86%E5%8D%9A%E5%AE%A2
Copy after login

所以说,我们将上面数组的name的值用urlencode编码以后,再进行json格式转化,那中文编码的问题就不存在了。

$data = array('id'=>1,'name'=>urlencode('迹忆博客'),'parId'=>0);$res = json_encode($data);echo $res;
Copy after login

现在我们得到的结果就是name编码以后的json字符串

{"id":1,"name":"%E8%BF%B9%E5%BF%86%E5%8D%9A%E5%AE%A2","parId":0}
Copy after login

最后再通过urldecode对url编码的字符串进行解码。当然urldecode解码不用我们再去找对应的那一段编码的字符串进行解码。我们可以直接对整个字符串进行url解码。urldecode会自动去检测相应的url编码的字符串对其进行解码。

$data = array('id'=>1,'name'=>urlencode('迹忆博客'),'parId'=>0);$res =urldecode(json_encode($data));echo $res;
Copy after login

这样其结果就是正确的了

{"id":1,"name":"迹忆博客","parId":0}
Copy after login
Copy after login

这种方法是没有版本限制的,但是效率的话肯定会相对于第一种方法要慢一些。

针对第二个方法——通过url编码解决中文问题——的封装函数

现在问题来了,对于第二个方法,我们总不能在数组的每一个中文前面都加上urlencode函数吧!这也是不现实的。那我们可以通过自己封装json_encode函数来处理这样的问题。

代码如下

/** * 作者:迹忆 * 个人博客:迹忆博客 * 博客url:www.onmpw.com*/function onmpw_json_encode($data){        if(is_object($data)) return false;        if(is_array($data)){            $data = deal_array($data);        }        return urldecode(json_encode($data));}function deal_array($data){     if (is_array($data)) {        foreach ($data as $key => $val) {            if (is_array($val)) {                                     //如果是多维数组,通过递归来处理多维数组                $data[$key] = deal_array($val);            } else {                                     //对值进行url编码                $data[$key] = urlencode($val);            }        }    } elseif (is_string($data)) {        $data = urlencode($data);    }    return $data;}
Copy after login

下面我们来看一个使用示例

$data = array(            array('id'=>1,'name'=>'迹忆博客','parId'=>0),            array('id'=>2,'name'=>'学无止境','parId'=>1),            array('id'=>3,'name'=>'趣味杂谈','parId'=>1),            array('id'=>4,'name'=>'编程语言','parId'=>2),            array('id'=>5,'name'=>'网络','parId'=>2),            array('id'=>6,'name'=>'算法','parId'=>2),            array('id'=>7,'name'=>'操作系统','parId'=>2),            array('id'=>8,'name'=>'数据库','parId'=>2),            array('id'=>9,'name'=>'WEB前端','parId'=>2),            array('id'=>10,'name'=>'读书','parId'=>3),            array('id'=>11,'name'=>'观点与感想','parId'=>3)        );$data = onmpw_json_encode($data);echo $data;
Copy after login

现在我们得到了我们想要的结果

[{"id":"1","name":"迹忆博客","parId":"0"},{"id":"2","name":"学无止境","parId":"1"},{"id":"3","name":"趣味杂谈","parId":"1"},{"id":"4","name":"编程语言","parId":"2"},{"id":"5","name":"网络","parId":"2"},{"id":"6","name":"算法","parId":"2"},{"id":"7","name":"操作系统","parId":"2"},{"id":"8","name":"数据库","parId":"2"},{"id":"9","name":"WEB前端","parId":"2"}, {"id":"10","name":"读书","parId":"3"},{"id":"11","name":"观点与感想","parId":"3"}]
Copy after login

总结

中文编码的问题很常见,我们大家要在实际情况中多总结。在以后的开发中再遇到类似的问题就可以很容易的解决了。

对于上面自定义的json_encode函数,我会把它封装到一个公共类中。源代码都在github上,我会持续将一些常用函数封装到这个公共类中,欢迎大家点击下载。希望这些对大家有所帮助。

想阅读更多技术文章,请访问听云技术博客,访问听云官方网站感受更多应用性能优化魔力。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles