zoukankan      html  css  js  c++  java
  • json_encode转义中文问题

    默认情况下php的 json_decode 方法会把特殊字符进行转义,还会把中文转为Unicode编码形式。

    这使得数据库查看文本变得很麻烦。所以我们需要限制对于中文的转义。

    对于PHP5.4+版本,json_decode函数第二个参数,可以用来限制转义范围。

    要限制中文,使用JSON_UNESCAPED_UNICODE参数。

          json_encode($a, JSON_UNESCAPED_UNICODE); 

    对于PHP5.3版本,可以先把ASCII 127以上的字符转换为HTML数值,这样避免被json_decode函数转码

    function my_json_encode($arr)  

    {

          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'); 

  • 相关阅读:
    django 2.0 path
    Django
    ORM
    Django简介
    web框架
    HTTP协议
    web应用
    索引
    pyMysql模块
    视图、触发器、存储过程、函数
  • 原文地址:https://www.cnblogs.com/gyrgyr/p/7244519.html
Copyright © 2011-2022 走看看