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

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

WBOY
WBOYOriginal
2016-06-06 20:38:592064browse

["/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()转化,结果为空。

<code>$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;
</code>

是什么情况啊。。。

回复内容:

["/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()转化,结果为空。

<code>$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;
</code>

是什么情况啊。。。

<code><?php function arrayEqual($array1,$array2){
    return !array_diff($array1,$array2) && !array_diff($array2,$array1);
}
</code></code>

楼主可以了解一下array_diff

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

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

<code><?php $a = "[\"/Public/editor/php/../attached/image/20150115/20150115094109_33178.jpg\",\"/Public/editor/php/../attached/image/20150115/20150115094110_19764.jpg\"]";
$b = json_decode($a);
var_dump($b);
</code></code>

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn