zoukankan      html  css  js  c++  java
  • php json_decode失败,返回null

      在使用json_decode之前,一定得保证字符串是utf-8编码,而执行json_decode失败的原因有很多,罗列如下:

          1)编码不对;

          2)字符串格式不对;

          3)字符串格式对,但是有异常字符;

          为了解决这个问题,可以考虑保证编码对上,json字符串可以正常解析,虽然说的简单,但是有许多工作要做,现在上一种万能解决方案,不啰嗦,看代码:

        

    // 获得编码,如果有其它编码,完善下面的编码列表即可
    $encode = mb_detect_encoding($json_info, array("ASCII","UTF-8","GB2312","GBK","BIG5","EUC-CN"));
    // 将字符串转为utf-8编码
     $tmp = iconv($encode,"UTF-8//IGNORE", $json_info);
    
    // reject overly long 2 byte sequences, as well as characters above U+10000 and replace with ?
     $tmp = preg_replace('/[x00-x08x10x0Bx0Cx0E-x19x7F]'.
                         '|[x00-x7F][x80-xBF]+'.
                          '|([xC0xC1]|[xF0-xFF])[x80-xBF]*'.
                           '|[xC2-xDF]((?![x80-xBF])|[x80-xBF]{2,})'.
                            '|[xE0-xEF](([x80-xBF](?![x80-xBF]))|(?![x80-xBF]{2})|[x80-xBF]{3,})/S',
                             '?', $tmp);
    
    // reject overly long 3 byte sequences and UTF-16 surrogates and replace with ?
     $tmp = preg_replace('/xE0[x80-x9F][x80-xBF]'.
                         '|xED[xA0-xBF][x80-xBF]/S','?', $tmp);
    // decode
    $result_data = json_decode($tmp,true);

    参考地址:

    https://magp.ie/2011/01/06/remove-non-utf8-characters-from-string-with-php/

    http://blog.sina.com.cn/s/blog_65db99840101fxzv.html

    https://segmentfault.com/a/1190000006154011

  • 相关阅读:
    洛谷 P2677 超级书架 2 题解
    洛谷 P3957 跳房子 二分+DP检验+单调队列优化
    BZOJ 1030 AC自动机+DP
    SPOJ-EPALIN 字符串哈希 回文
    URAL-1989 树状数组+字符串哈希
    POJ-2774 字符串哈希+二分
    CCF 201709-5 除法(线段树)
    CCF CSP个人题解汇总
    CCF CSP历年一二题代码汇总
    CCF 201803-4 棋局评估 (对抗搜索)
  • 原文地址:https://www.cnblogs.com/foreverstars/p/9093636.html
Copyright © 2011-2022 走看看