分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

php json_decode失败,返回null

发布时间:2023-09-06 01:56责任编辑:傅花花关键词:jsjson

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

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved