gpt4 book ai didi

php - 自动将尾部斜杠添加到 urlencoded url

转载 作者:可可西里 更新时间:2023-11-01 01:09:44 27 4
gpt4 key购买 nike

我对以下内容很困惑:

echo("<a href='http://".urlencode("www.test.com/test.php?x=1&y=2")."'>test</a><br>");

echo("<a href='http://"."www.test.com/test.php?x=1&y=2"."'>test</a>");

第一个链接添加了尾部斜线(这给我带来了问题)第二个链接没有。

任何人都可以帮助我理解为什么。显然它似乎与 urlencode 有关,但我无法找出是什么。

谢谢

最佳答案

您不应该使用 urlencode() 来回显 URL,除非它们包含一些非标准字符。

所提供的示例不包含任何异常。

例子

$query = 'hello how are you?';

echo 'http://example.com/?q=' . urlencode($query);
// Ouputs http://example.com/?q=hello+how+are+you%3F

我使用它是因为 $query 变量可能包含空格、问号等。我不能使用问号,因为它表示查询字符串的开头,例如index.php?page=1

事实上,该示例最好直接输出而不是echo

此外,当我尝试您的示例代码时,我没有得到一个反斜杠,实际上我得到了

<a href='http://www.test.com%2Ftest.php%3Fx%3D1%26y%3D2'>test</a>

关于php - 自动将尾部斜杠添加到 urlencoded url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3830313/

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