PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

php接受表单提交的json字符串多了反斜杠,遇到一些处理字符串的问题,求解答

原创
2016-06-06 20:38:59 1842浏览

["/Public/editor/php/../attached/image/20150115/20150115094109_33178.jpg","/Public/editor/php/../attached/image/20150115/20150115094110_19764.jpg"]
表单提交上面字符串,
php接收并输出,结果是[\"/Public/editor/php/../attached/image/20150115/20150115094109_33178.jpg\",\"/Public/editor/php/../attached/image/20150115/20150115094110_19764.jpg\"]

百度了一下,使用stripslashes把反斜杠去掉后使用json_decode()转化,结果为空。

$img=stripslashes($data['img']);
$img_list=json_decode($img);
$test='["/Public/editor/php/../attached/image/20150115/20150115094109_33178.jpg","/Public/editor/php/../attached/image/20150115/20150115094110_19764.jpg"]';
//$test内容为输入框里的字符串
if($test == $img) echo 1; //if语句不成立,没有输出1;

是什么情况啊。。。

回复内容:

["/Public/editor/php/../attached/image/20150115/20150115094109_33178.jpg","/Public/editor/php/../attached/image/20150115/20150115094110_19764.jpg"]
表单提交上面字符串,
php接收并输出,结果是[\"/Public/editor/php/../attached/image/20150115/20150115094109_33178.jpg\",\"/Public/editor/php/../attached/image/20150115/20150115094110_19764.jpg\"]

百度了一下,使用stripslashes把反斜杠去掉后使用json_decode()转化,结果为空。

$img=stripslashes($data['img']);
$img_list=json_decode($img);
$test='["/Public/editor/php/../attached/image/20150115/20150115094109_33178.jpg","/Public/editor/php/../attached/image/20150115/20150115094110_19764.jpg"]';
//$test内容为输入框里的字符串
if($test == $img) echo 1; //if语句不成立,没有输出1;

是什么情况啊。。。

楼主可以了解一下array_diff

stripslashes后var_dump看下,是否是合法的json字串。 是不是有实体的双引号 ?

谢谢@star001007 提醒,其实那个引号不需要过滤,json字符串本来就是需要用""引起来的。提主直接json_decode就可以了

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。