码迷,mamicode.com
首页 > Web开发 > 详细

php json_decode失败,返回null

时间:2018-05-26 17:53:24      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:.com   result   reg   解析   char   地址   异常   har   了解   

  在使用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-\x08\x10\x0B\x0C\x0E-\x19\x7F]‘.
                     ‘|[\x00-\x7F][\x80-\xBF]+‘.
                      ‘|([\xC0\xC1]|[\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

 

php json_decode失败,返回null

标签:.com   result   reg   解析   char   地址   异常   har   了解   

原文地址:https://www.cnblogs.com/foreverstars/p/9093636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!