php 斜线问题?使用 json_encode。为什么以及如何解决?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6743554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Slash issue?with json_encode. Why and how to solve it?
提问by Me hdi
Why outputting this date ("2011/7/11")
with json_encode
displays ("2011\/7\/11")
?
为什么("2011/7/11")
用json_encode
显示器输出这个日期("2011\/7\/11")
?
How can I convert "2011\/7\/11"
to "2011/7/11"
?
我怎样才能转换"2011\/7\/11"
为"2011/7/11"
?
$data_go = '2011/7/11';
$ddmmyyy='([1-9][\d]{3})[- \/.]([0-1][\d])[- \/.]([0-3][\d])';
if(preg_match("/$ddmmyyy$/", $data_go)) {
$year = substr($data_go,0,4);
$month = substr($data_go,5,2);
$day = substr($data_go,8,2);
$j2g = $this->convert_date->JalaliToGregorian($year, $month, $day);
$ok = $j2g[0]."/".$j2g[1]."/".$j2g[2];
}else {
return FALSE;
}
echo json_encode($ok); // output "2011\/7\/11"
回答by Artefacto
In PHP 5.4, you can use JSON_UNESCAPED_SLASHES
:
在 PHP 5.4 中,您可以使用JSON_UNESCAPED_SLASHES
:
echo json_encode("2011/7/11", JSON_UNESCAPED_SLASHES);
Otherwise, you have to do some trivial post-processing
否则,你必须做一些琐碎的后处理
str_replace('\/', '/', json_encode("2011/7/11"));
Note that \/
is a valid way to represent /
in JSON.
请注意,这\/
是一种/
以 JSON表示的有效方式。
回答by Rukmi Patel
$data_go = str_replace('\'','', $data_go);
it will remove all forwards slashes and will give you desired result..
它将删除所有正斜杠,并会给你想要的结果..