dongyan1491 2019-08-19 10:33
浏览 159
已采纳

如何在php中将url(http:/ \ / \ test)解码为(http:// test)?

I'm making a form in HTML and inside this form, there is some input text like URL, parameter, radio button and inside the radio button also have the same input like URL and parameter. After submitting my form, I want these data to save in a file format .txt, but I found a problem that URL saved in this file is http:/\/\test but if I show the data submitted with var_dump($data) URL is showing in http://test

I tried to use urldecode($data) but the result is still the same

if (isset($_POST['submit'])) {
    $nameURL = $_POST['nameURL'];
    $param = $_POST['param'];

    if($_POST['radiobutton']=='asynchronous'){
        $asyncURL = $_POST['asyncURL'];
        $asyncParam = $_POST['asyncParam'];

        $data = array(
            "Callback" => urldecode($nameURL),
            "Param" => $param,
            "asynchronous" => array(
                "Callback" => $asyncURL = urldecode($asyncURL),
                "Param" => array(
                            $asyncParam
                           )
            )
        );
    }else if($_POST['radiobutton']=='synchronous'){
        $syncURL = $_POST['syncURL'];
        $syncParam = $_POST['syncParam'];

        $data = array(
            "Callback" => urldecode($nameURL),
            "Param" => $param,
            "synchronous" => array(
                "Callback" => urldecode($syncURL),
                "Param" => array(
                            $syncParam
                           )
            )
        );
    }

    $data = json_encode($data);

    $filename = date('YmdHis').".txt";
    if(!file_exists($filename)){
        $fh = fopen($filename, 'w') or die("Can't create file!");
    }
    $ret = file_put_contents($filename, $data, FILE_APPEND | LOCK_EX);
    if($ret == false){
        die('There was an error writing this file');
    }

and my HTML form

    <form id="myform">
        <div class="form-group">
            <input type="url" name="nameURL" id="nameURL" placeholder="URL">
            <input type="text" id="param" name="param[]" placeholder="Param">
            <input type="button" onclick="addRow()" value="Create Row"/>
        </div>
        <div class="form-group">
            <label><input type="radio" name="radiobutton" value="asynchronous" id="asynchronous">Asynchronous</label>
            <label><input type="radio" name="radiobutton" value="synchronous" id="synchronous">Synchronous</label>
            <div class="asynchronous">
                <div class="form-group">
                    <input type="text" name="asyncURL" id="asyncURL" placeholder="Callback URL">
                    <input type="text" id="asyncParam" name="asyncParam[]" placeholder="Param">
                    <input type="button" onclick="createRowAsyc()" value="Create Row" />
                </div>
            </div>
            <div class="synchronous">
                <div class="form-group">
                    <input type="text" id="syncParam" name="syncParam[]" placeholder="Param">
                    <input type="button" onclick="createRowSync()" value="Create Row" />
                </div>
            </div>
        </div>
        <button type="submit" name="submit" id="submit">Submit</button>
    </form>

I expect the output is http://test but the output saved in the file is http:/\/\test

please help me solve my problem or anything can help this

  • 写回答

2条回答 默认 最新

  • douxing6532 2019-08-19 10:40
    关注

    You can add JSON_UNESCAPED_SLASHES constant to the json_encode() like so:

     json_encode($data, JSON_UNESCAPED_SLASHES);
    

    Which, for the following input:

    $data = json_encode([
        "url" => "http://example.com"
    ], JSON_UNESCAPED_SLASHES);
    

    will output:

    {"url":"http://example.com"}
    

    As per the documentation, this will not escape the slashes:

    JSON_UNESCAPED_SLASHES Don't escape /. Available as of PHP 5.4.0.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?