在使用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