php json encode中文乱码,php json_encode中文乱码如何解决

php encode中文乱码的解决办法:首先打开相应的PHP文件;然后使用正则语句“preg_replace("#\\\u([0-9a-f]{4})#ie","iconv('UCS-2BE', 'UTF-8'...)”将编码替换成中文即可。

fbf2b3a9f1dd9669536b749e0256409e.png

本文列举3个方法,实现json_encode()后的string显示中文问题。

做接口时不需要,但存log时帮了大忙了。

在贴代码前,必须贴上官方param和return,链接:http://php.net/manual/zh/function.json-encode.php

参数value

待编码的 value ,除了resource类型之外,可以为任何数据类型

该函数只能接受 UTF-8 编码的数据

options

由以下常量组成的二进制掩码: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS,JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT,JSON_UNESCAPED_UNICODE.

返回值

编码成功则返回一个以 JSON 形式表示的 string 或者在失败时返回 FALSE。<?php

// json_encode() 保持中文方法详解

$arr['city'] = '北京';

$arr['name'] = 'weilong';

// 直接输出

// Res: {"city":"\u5317\u4eac","name":"weilong"}

echo json_encode($arr), "\n";

#### 1. 加参数,PHP版本>=5.4

// Res: {"city":"北京","name":"weilong"}

echo json_encode($arr, JSON_UNESCAPED_UNICODE), "\n"; // php >= 5.4

#### 2. 正则替换,json_encode后,正则将编码替换成中文

// Res: {"city":"北京","name":"weilong"}

echo preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", json_encode($arr)), "\n"; // PHP 5.5 /e修饰符被弃用

echo preg_replace_callback("/\\\u([0-9a-f]{4})/i", function($match) { // php >= 5.3 都可以

return json_decode("\"{$match[0]}\"", true);

}, json_encode($arr)), "\n";

#### 3. urldecode()、urlencode()函数,不推荐

// Res1: null, Res2: {"city":"北京","name":"weilong"}

echo urldecode(json_encode(urlencode($arr))), "\n";

$arr['city'] = urlencode($arr['city']); // urlencode()参数必须是string

echo urldecode(json_encode($arr)), "\n";

// 另外注意json_decode()参数区别。

$arr['city'] = '北京';

$arr['name'] = 'weilong';

$str = json_encode($arr);

$str2 = json_decode($str);

$str3 = json_decode($str, true);

print_r($str2); // object

/* Res:

stdClass Object

(

[city] => 北京

[name] => weilong

) */

print_r($str3); // array

/* Res:

Array

(

[city] => 北京

[name] => weilong

)

*/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541768.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

乡村图景(转载)

转自: http://cul.qq.com/a/20160205/046437.htm 我丈夫家在湖北孝感孝昌县的一个村子。2005年第一次过年回到他家&#xff0c;印象最深的就是嫂子。我暗自问当时的男友&#xff0c;“哥哥尽管算不上特别帅气&#xff0c;但为何找了这么难看的嫂子&#xff1f;”后来才发现&…

stl向量最大值_C ++ STL中向量的最小和最大元素

stl向量最大值Given a vector and we have to find the smallest (minimum) and largest (maximum) elements. 给定一个向量&#xff0c;我们必须找到最小(最小)和最大(最大)元素。 查找向量的最小和最大元素 (Finding vectors minimum & maximum elements) To find minim…

oracle如何设置备份计划任务,Oracle数据库设置任务计划备份一周的备份记录

Oracle 数据库备份&#xff1a;--保留最近一周的备份记录&#xff1b;正文&#xff1a;开始代码如下:echo 设置备份文件存放文件夹...set "tbufE:\Cway\backup"echo 设置备份文件名(以星期几命名&#xff0c;即备份文件只保存最近一周)...set name%date%set name%nam…

索引(转载自百度百科)

Oracle索引 编辑本词条缺少信息栏、名片图&#xff0c;补充相关内容使词条更完整&#xff0c;还能快速升级&#xff0c;赶紧来编辑吧&#xff01;在oracle索引是一种供服务器在表中快速查找一个行的数据库结构。合理使用索引能够大大提高数据库的运行效率。目录 1 概念及作用 2…

阿姆斯特朗数_阿姆斯特朗的功能依赖公理 数据库管理系统

阿姆斯特朗数Armstrong axioms are a complete set of inference rules or axioms, introduced and developed by William W. Armstrong in 1974. The inference rules are sound which is used to test logical inferences of functional dependencies. The axiom which also …

ORACLE JOB 失败 查看,Oracle JOB异常中断原因分析

注释今天研发同事找我确认 PKG_WMS.proc_TaskMain 存储的 job 是否还在运行&#xff0c;竟发现 dba_jobs.NEXT_DATE4000/1/1&#xff0c;如下看看究竟原因吧~JOB 信息&#xff1a;参数&#xff1a;BROKEN : 中断标记 ,N 启动、Y 中断 --> DBMS_JOBS.BROKEN(job_id,TRUE/FA…

ruby打印_Ruby程序打印一个数字的乘法表

ruby打印打印乘法表 (Printing multiplication table) This requires a very simple logic where we only have to multiply the number with digits from 1 to 10. This can be implemented by putting the multiplication statement inside a loop. We have mentioned two wa…

步骤1:JMeter 录制脚本接口测试

JMeter 常用测试方法简介 1.下载安装 http://jmeter.apache.org/download_jmeter.cgi 安装JDK&#xff0c;配置环境变量JAVA_HOME. 系统要求&#xff1a;JMeter2.11 需要JDK1.6以上的版本支持运行 2.学习Jmeter元件 http://jmeter.apache.org/usermanual/component_reference.h…

模拟断电oracle数据不一致,Oracle数据库案例整理-Oracle系统运行时故障-断电导致数据文件状态变为RECOVER...

1.1 现象描述异常断电&#xff0c;数据库数据文件的状态由ONLINE变为RECOVER。系统显示如下信息&#xff1a;SQL> select file_name ,tablespace_name ,online_status from dba_data_files;FILE_NAME---------------------------------------------------------------…

python日历模块_Python日历模块| prmonth()方法与示例

python日历模块Python calendar.prmonth()方法 (Python calendar.prmonth() Method) prmonth() method is an inbuilt method of the calendar module in Python. It works on simple text calendars and prints the calendar of the given month of the given year. Also, the…

多例模式

多例&#xff1a;只是单例的一种延伸 不必过于在意各种模式的名字&#xff0c;重要的是学会融会贯通&#xff0c;把生产的car放到集合中 类似JDBC 的连接池 把连接对象放到池中 多例模式特点&#xff1a; 1. 多例类可以有多个实例 2. 多例类必须自己创建自己的实例&a…

Oracle public view,【易错概念】以太坊Solidity函数的external/internal,public/private,view/pure/payable区别...

1. 函数类型&#xff1a;内部(internal)函数和外部(external)函数函数类型是一种表示函数的类型。可以将一个函数赋值给另一个函数类型的变量&#xff0c;也可以将一个函数作为参数进行传递&#xff0c;还能在函数调用中返回函数类型变量。 函数类型有两类&#xff1a;- 内部(i…

c-style字符字符串_C字符串-能力问题与解答

c-style字符字符串C programming String Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on Strings, String is the set of characters and String related Aptitude Questions and Answers you will find here. C编程Stri…

PHP Smarty template for website

/******************************************************************************* PHP Smarty template for website* 说明&#xff1a;* 之前一直在想将MVC的方式加在PHP做的网站上&#xff0c;这样比较好处理&#xff0c;相对来说比较好* 处理…

ftp连接oracle服务器,使用SSL加密连接FTP - 架建SSL安全加密的FTP服务器(图)_服务器应用_Linux公社-Linux系统门户网站...

四、使用SSL加密连接FTP启用Serv-U服务器的SSL功能后&#xff0c;就可以利用此功能安全传输数据了&#xff0c;但FTP客户端程序必须支持SSL功能才行。 如果我们直接使用IE浏览器进行登录则会出现图4显示的错误信息&#xff0c;一方面是以为没有修改默认的端口21为990&#xff0…

c# 情感倾向_C否则-能力倾向问题与解答

c# 情感倾向C programming if else Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on condition statements – if else, nested if else, ladder if else, conditional operators etc. C语言编程如果有问题&#xff0c;请…

springboot中使用缓存shiro-ehcache

在pom.xml中注入缓存依赖&#xff0c;版本(Sep 09, 2016)spring-context-support 包含支持UI模版&#xff08;Velocity&#xff0c;FreeMarker&#xff0c;JasperReports&#xff09;&#xff0c; 邮件服务&#xff0c; 脚本服务(JRuby)&#xff0c; 缓存Cache&#xff08;EHCa…

oracle 微信公众号,关于微信公众号贴代码的方法

微信公众号码上贴代码一直一来都是个头疼的问题。吐槽一句&#xff1a;要是后台编辑器支持markdown就好了。今天教大家用在线markdown排版工具&#xff0c;把代码完美贴到微信公众号上。长话短说&#xff0c;今天用到的两个工具&#xff1a;首先&#xff0c;以一段代码为例。假…

计算理论 形式语言与自动机_下推式自动机(PDA)| 计算理论

计算理论 形式语言与自动机Pushdown Automaton (PDA) is a kind of Automaton which comes under the theory of Computation that appoints stack. The word Pushdown stands due to the fact that the stack can be pushed down as operations can only work on the elements…

运维人员究竟如何提升价值,持续获得高薪?

作者简介&#xff1a;老男孩&#xff0c;北京老男孩IT教育创始人&#xff0c;17年IT经验&#xff0c;资深Linux实战专家&#xff0c;IT培训界实战派顶尖大师&#xff0c;国内将实战心理学体系大量注入IT运维培训领域的第一人&#xff0c;多本IT畅销图书作者&#xff0c;51CTO金…