Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

php - Slash issue with json_encode. Why and how to solve it?

Why outputting this date ("2011/7/11") with json_encode displays ("2011/7/11")?

How can I convert "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"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"));

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...