gpt4 book ai didi

php - 如何解码 url (http :/\/\test) into (http://test) in php?

转载 作者:太空宇宙 更新时间:2023-11-04 13:27:32 24 4
gpt4 key购买 nike

我正在用 HTML 制作一个表单,在这个表单中,有一些输入文本,如 URL、参数、单选按钮,单选按钮内部也有相同的输入,如 URL 和参数。提交表单后,我希望将这些数据保存为.txt文件格式,但我发现一个问题,该文件中保存的URL是 http:/\/\test 但是如果我显示数据使用 var_dump($data) 提交 URL 显示在 http://test

我尝试使用 urldecode($data) 但结果还是一样

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');
}

和我的 HTML 表单

    <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>

我希望输出是 http://test 但保存在文件中的输出是 http:/\/\test

请帮我解决我的问题或者有什么可以帮助的

最佳答案

您可以将 JSON_UNESCAPED_SLASHES 常量添加到 json_encode() 中,如下所示:

 json_encode($data, JSON_UNESCAPED_SLASHES);

其中,对于以下输入:

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

将输出:

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

根据 documentation , 这不会逃脱斜杠:

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

关于php - 如何解码 url (http :/\/\test) into (http://test) in php?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57555003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com