替换php响应的字符串

时间:2016-03-09 11:14:28

标签: php json response

我正在使用这样的php文件..

<?php

        define('HOST','localhost');
        define('USER','root');
        define('PASS','123');
        define('DB','123');

        $mysqli = new mysqli(HOST,USER,PASS,DB);
        /* check connection */
        if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
        }       

        $mysqli->query("SET NAMES 'utf8'");
        //$sql="SELECT via,events,doc FROM profile";
                $sql="SELECT via, doc FROM profile";
        $result=$mysqli->query($sql);
        while($e=mysqli_fetch_assoc($result)){
        $output["response"][]=$e;

        //json_encode($output) = str_replace("\/", '/', json_encode($output));/* not working*/
        }   

        echo(json_encode($output)); 
        $mysqli->close();   

        ?>

它正在给出这样的回应。

{"response":[{"via":"19156001882722","doc":"http:\/\/oursite\/propic_uploads\/58.png"},{"via":"sunil holla","doc":"propic_uploads\/Fantasia Painting(29).jpg"}]}

我怎么能得到这样的上述回答。我的意思是如何用这个回答取代上述回应。

{"response":[{"via":"19156001882722","doc":"http://oursite/propic_uploads/58.png"},{"via":"sunil holla","doc":"propic_uploads/Fantasia Painting(29).jpg"}]}

任何人都可以帮我解决这个问题......

2 个答案:

答案 0 :(得分:3)

这里已经解决了:json_encode() escaping forward slashes

您只需要使用JSON_UNESCAPED_SLASHES标志:

json_encode($str, JSON_UNESCAPED_SLASHES);

答案 1 :(得分:0)

您需要stripslashes()函数来处理响应中的条带斜杠。

示例:

$string = '{"response":[{"via":"19156001882722","doc":"http:\/\/oursite\/propic_uploads\/58.png"},{"via":"sunil holla","doc":"propic_uploads\/Fantasia Painting(29).jpg"}]}';    
echo stripslashes($string);

<强>结果:

{"response":[{"via":"19156001882722","doc":"http://oursite/propic_uploads/58.png"},{"via":"sunil holla","doc":"propic_uploads/Fantasia Painting(29).jpg"}]}