源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

PHP实现json_decode不转义中文的方法

  • 时间:2020-12-04 06:21 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:PHP实现json_decode不转义中文的方法
本文实例讲述了PHP实现json_decode不转义中文的方法。分享给大家供大家参考,具体如下: 默认情况下PHP的 json_decode 方法会把特殊字符进行转义,还会把中文转为[code]Unicode[/code]编码形式。 这使得数据库查看文本变得很麻烦。所以我们需要限制对于中文的转义。 [b]对于PHP5.4+版本,json_decode函数第二个参数,可以用来限制转义范围。[/b] 要限制中文,使用[code]JSON_UNESCAPED_UNICODE[/code]参数。
json_encode($a, JSON_UNESCAPED_UNICODE);

[b]对于PHP5.3版本,可以先把ASCII 127以上的字符转换为HTML数值,这样避免被json_decode函数转码:[/b]
function my_json_encode($arr)
{
    //convmap since 0x80 char codes so it takes all multibyte codes (above ASCII 127). So such characters are being "hidden" from normal json_encoding
    array_walk_recursive($arr, function (&$item, $key) { if (is_string($item)) $item = mb_encode_numericentity($item, array (0x80, 0xffff, 0, 0xffff), 'UTF-8'); });
    return mb_decode_numericentity(json_encode($arr), array (0x80, 0xffff, 0, 0xffff), 'UTF-8');
}

[b]PS:这里再为大家推荐几款比较实用的json在线工具供大家参考使用:[/b] [b]在线JSON代码检验、检验、美化、格式化工具: [/b][url=http://tools.jb51.net/code/json]http://tools.jb51.net/code/json[/url] [b]JSON[/b][b]在线格式化工具: [/b][url=http://tools.jb51.net/code/jsonformat]http://tools.jb51.net/code/jsonformat[/url] [b]在线XML/JSON互相转换工具: [/b][url=http://tools.jb51.net/code/xmljson]http://tools.jb51.net/code/xmljson[/url] [b]json[/b][b]代码在线格式化/美化/压缩/编辑/转换工具: [/b][url=http://tools.jb51.net/code/jsoncodeformat]http://tools.jb51.net/code/jsoncodeformat[/url] [b]C语言风格/HTML/CSS/json代码格式化美化工具: [/b][url=http://tools.jb51.net/code/ccode_html_css_json]http://tools.jb51.net/code/ccode_html_css_json[/url] 更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/239.htm]PHP中json格式数据操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/630.htm]PHP数学运算技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/348.htm]PHP基本语法入门教程[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部