Home > Backend Development > PHP Tutorial > PHP json encoding method without escaping Chinese characters_PHP tutorial

PHP json encoding method without escaping Chinese characters_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:27:41
Original
1003 people have browsed it

Although the latest PHP 5.4 already supports JSON Chinese encoding, that is, through the JSON_UNESCAPED_UNICODE parameter, for example:

json_encode("Chinese", JSON_UNESCAPED_UNICODE) For earlier PHP versions, it handles json encoding without escaping Chinese characters. I have written an article about PHP outputting Chinese JSON strings before. Here is a more perfect method:

 /**

* JSON encoding method without escaping Chinese characters

* @param array $arr Array to be encoded

* @return string

*/

Function encode ($arr) {

$str = json_encode($arr);

 $search = "#\u([0-9a-f]+)#ie";

 $replace = "iconv('UCS-2', 'UTF-8', pack('H4', '\1'))";

return preg_replace($search, $replace, $str);

 }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/815789.htmlTechArticleAlthough the latest PHP 5.4 already supports JSON Chinese encoding, that is, through the JSON_UNESCAPED_UNICODE parameter, for example: json_encode(Chinese, JSON_UNESCAPED_UNICODE )For earlier PHP versions,...
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template