Me *_*hdi 15 php json codeigniter date
为什么("2011/7/11")用json_encode显示器输出这个日期("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"
Run Code Online (Sandbox Code Playgroud)
Art*_*cto 37
在PHP 5.4中,您可以使用JSON_UNESCAPED_SLASHES:
echo json_encode("2011/7/11", JSON_UNESCAPED_SLASHES);
Run Code Online (Sandbox Code Playgroud)
否则,你必须做一些简单的后处理
str_replace('\\/', '/', json_encode("2011/7/11"));
Run Code Online (Sandbox Code Playgroud)
请注意,这\/是/在JSON中表示的有效方式.