PHP:从url中获取所有参数,但不包含文件id


PHP: Get all parameters from the url without the file id

我想将链接中所有使用的参数提取为文本字符串。示例:

$link2 = http://example.com/index.html?song=abcdefg;

当使用上述链接时,$param应给出所有参数"?song=abcdefg'。不幸的是,我不知道id index.html,也不知道参数和它们各自的数据值。

据我所知,有一个函数$_GET,它创建了一个数组,但我需要一个字符串。

您可以使用parse_url:

$link2 = 'http://example.com/index.html?song=abcdefg';
$param = '?' . parse_url($link2, PHP_URL_QUERY);
echo $param;
// ?song=abcdefg

存在许多库来解析url,您可以使用此库作为示例:

https://github.com/thephpleague/uri

use League'Uri'Schemes'Http as HttpUri;
$link2 = 'http://example.com/index.html?song=abcdefg';
$uri = HttpUri::createFromString($link2);
// then you can access the query
$query = $uri->query;

你也可以试试这个:https://github.com/jwage/purl

一种奇怪的方法是

$link2 = 'http://example.com/index.html?song=abcdefg';
$param = strstr($link2, "?");
echo $param // ?song=abcdefg

CCD_ 4将在CCD_ 5的第一个位置之后得到所有;包括领先的?

您可以循环遍历get数组并将其解析为字符串:

$str = "?"
foreach ($_GET as $key => $value) { 
    $temp = $key . "=". $value . "&";
    $str .= $temp
}
rtrim($str, "&")//remove leading '&'

您可以使用http_build_query()方法

if ( isset ($_GET))
{
    $params =  http_build_query($_GET);     
}
// echo $params should return "song=abcdefg";