问题:当我们字符串含有特殊字符的情况下,我们该如何巧妙利用json_decode来解析字符串呢?
案例:
<?php $s = '[["3807913","□iha","2012-12-16","2012-12-16","127.0.0.1","0","0","0","0","0"],["3807913","大家好","2012-12-16","2012-12-16","127.0.0.1","0","0","0","0","0"]]'; $arr = json_decode($s,TRUE);//解析失败,由于$s字符串含有特殊字符 print_r($arr); ?>解决方法
<?php $s = '[["3807913","□iha","2012-12-16","2012-12-16","127.0.0.1","0","0","0","0","0"],["3807913","大家好","2012-12-16","2012-12-16","127.0.0.1","0","0","0","0","0"]]'; $s = substr(str_replace('\"','"',json_encode($s)),1,-1); $arr = json_decode($s,TRUE);//解析成功 print_r($arr); ?>