从URL获取$ _GET []参数部分

时间:2013-09-13 19:04:00

标签: php url get

我有这种类型的网址:

http://mysite.com/results.php?location=montreal&car-rental=no&add-hotel=no&moods%5B%5D=Adventure&moods%5B%5D=Arts&moods%5B%5D=Culture&moods%5B%5D=Well-being&moods%5B%5D=Zen&weather-dependent=yes

在PHP中,获得$_GET[]部分的最佳方式是什么?

例如:

$get = location=montreal&car-rental=no&add-hotel=no&moods%5B%5D=Adventure&moods%5B%5D=Arts&moods%5B%5D=Culture&moods%5B%5D=Well-being&moods%5B%5D=Zen&weather-dependent=yes

感谢。

2 个答案:

答案 0 :(得分:4)

您是否在搜索:

$_SERVER['QUERY_STRING']

答案 1 :(得分:2)

我想你正在寻找这个:

$str = 'http://mysite.com/results.php?location=montreal&car-rental=no&add-hotel=no&moods%5B%5D=Adventure&moods%5B%5D=Arts&moods%5B%5D=Culture&moods%5B%5D=Well-being&moods%5B%5D=Zen&weather-dependent=yes';

$query = parse_url($str, PHP_URL_QUERY);
parse_str($query, $arr);
print_r($arr);

/*
Array
(
    [location] => montreal
    [car-rental] => no
    [add-hotel] => no
    [moods] => Array
        (
            [0] => Adventure
            [1] => Arts
            [2] => Culture
            [3] => Well-being
            [4] => Zen
        )

    [weather-dependent] => yes
)
*/

Codepad

说明:parse_url($str, PHP_URL_QUERY)从该网址中提取查询字符串,然后parse_str($query, $arr)解析它,用$arr =>填充key变量value对{/ p>