所以我试图从地址栏中获取一个如下所示的 url:
http://mysite.com/url.php?name=http://test.com/format.jsp?id=738ths3&secure=false
我使用 $_GET 变量直接从 URL 读取它我的代码如下
$arc = rawurlencode($_GET['name']);
echo "URL: $arc";
这只会返回
URL: http://imgur.com/format.jsp?id=738ths3
它缺少 &secure=false
我想要它的样子:
URL: http://test.com/format.jsp?id=738ths3&secure=false
我试过 urlencode、rawurlencode 都无济于事,我在谷歌中查看了一些论坛和 stackoverflow 的答案都没有帮助,有什么想法吗?谢谢!
urlencode 显示:
网址:http%3A%2F%2Ftest.com
所以我也不能拥有那个!
最佳答案
在构造 URL 之前,您需要 urlencode()
,即:
$url = "http://mysite.com/url.php?name=".urlencode('http://test.com/format.jsp?id=738ths3&secure=false');
这样,您将能够从 $_GET['name']
获取完整的 URL 作为 name
GET 参数。
说明:
在构造 URL 时如果没有 urlencode()
,PHP 会将 is 视为 2 个单独的参数,由 &
分隔:
$_GET['名字']
这是http://imgur.com/format.jsp?id=738ths3
适合你的情况$_GET['secure']
对于您的情况,这是false
或者:
从您的评论来看,您似乎无法控制 URL 构造。您可以使用 http_build_query
在单个字符串中获取完整的 $_GET
:
$name = http_build_query($_GET);
然后您将获得:
echo $name; // name=http://test.com/format.jsp?id=738ths3&secure=false
// which you would then may want to strip away the first 'name='
$name = substr($name, strlen('name='));
echo $name; // to obtain http://test.com/format.jsp?id=738ths3&secure=false
关于php - 编码 $_GET 变量 URL 忽略符号 "&"字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11698324/