前端传过来的json数据有时候会被转化成
$str='[{"srctype":"0","srcurl":""},{"srctype":"0","srcurl":""},{"srctype":"0","srcurl":""},{"srctype":"0","srcurl":""},{"srctype":"0","srcurl":"http://shimao-smt.oss-cn-hangzhou.aliyuncs.com/1539862260910574983338757651936.jpg"}]';
//json_decode($str);会将json字符串转化成object对象形式, json_decode($str,true);则会转化成数组
这个时候使用 json_decode($str,true);是不会成功的,只会输出null,这个时候就需要转化被编译的单双引号了
$str='[{"srctype":"0","srcurl":""},{"srctype":"0","srcurl":""},{"srctype":"0","srcurl":""},{"srctype":"0","srcurl":""},{"srctype":"0","srcurl":"http://shimao-smt.oss-cn-hangzhou.aliyuncs.com/1539862260910574983338757651936.jpg"}]'; echo htmlspecialchars_decode($str);
$str将会被转化成正常的json字符串,如
$str=[{"srctype":"0","srcurl":""},{"srctype":"0","srcurl":""},{"srctype":"0","srcurl":""},{"srctype":"0","srcurl":""},{"srctype":"0","srcurl":"http://shimao-smt.oss-cn-hangzhou.aliyuncs.com/1539862260910574983338757651936.jpg"}]
这个时候就可以使用 json_decode($str,true);将其转化成数组了;
返回过来将单双引号咋转化成html格式化
使用php函数 htmlspecialchars($str);