php中json_encode不兼容JSON_UNESCAPED_UNICODE的解决方案,jsonencodeunicode
php中json_encode不兼容JSON_UNESCAPED_UNICODE的解决方案,jsonencodeunicode
PHP5.4才支持JSON_UNESCAPED_UNICODE这个参数,此参数是让中文字符在json_encode的时候不用转义,减少数据传输量。但在PHP5.3中,就得自己写个函数来实现,以下就是解决方法:
/** * 对变量进行 JSON 编码 * @param mixed value 待编码的 value ,除了resource 类型之外,可以为任何数据类型,该函数只能接受 UTF-8 编码的数据 * @return string 返回 value 值的 JSON 形式 */ function json_encode_ex($value) { if (version_compare(PHP_VERSION,'5.4.0','<')) { $str = json_encode($value); $str = preg_replace_callback( "#\\\u([0-9a-f]{4})#i", function($matchs) { return iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1])); }, $str ); return $str; } else { return json_encode($value, JSON_UNESCAPED_UNICODE); } }
判断PHP的版本,如果低于5.4则使用自定义函数来实现,如果是5.4及以上版本则使用JSON_UNESCAPED_UNICODE。
以上就是php中json_encode不兼容JSON_UNESCAPED_UNICODE的解决方案 ,希望能给大家一个参考,了解更多PHP的语法,大家可以关注《PHP5在线手册》,也希望大家多多支持帮客之家。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
