问题斜杠与json_encode。为什么和如何解决呢? [英] problem slash with json_encode. why and how solve it?

查看:144
本文介绍了问题斜杠与json_encode。为什么和如何解决呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么输出此日期(2011/7/11) whit json_encode显示数据(2011 \ / 7 \ / 11)
如何将2011 \ / 7 \ / 11转换为2011/7/11

why whan output this date ("2011/7/11") whit json_encode displaying data ("2011\/7\/11")? how can converted "2011\/7\/11" to "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"


推荐答案

在PHP 5.4中,您可以使用 JSON_UNESCAPED_SLASHES

In PHP 5.4, you can use 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"));

请注意, \ / 在JSON中表示 / 的方式。

Note that \/ is a valid way to represent / in JSON.

这篇关于问题斜杠与json_encode。为什么和如何解决呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆