Get the GET variables from a URL String

别来无恙 提交于 2019-11-27 20:14:15

parse_str(parse_url($url, PHP_URL_QUERY), $array), see the manpage for parse_str for more info.

$href = 'http://www.somesite.com/index.php?url=var&file_id=var&test=var';

$url = parse_url($href);
print_r($url);
/* Array
(
    [scheme] => http
    [host] => www.somesite.com
    [path] => /index.php
    [query] => url=var&file_id=var&test=var
) */

$query = array();
parse_str($url['query'], $query);

print_r($query);
/* Array
(
    [url] => var
    [file_id] => var
    [test] => var
) */

It's actually a lot easier than writing any custom functions.

$queryStr = $_SERVER['QUERY_STRING'];

I'd use something like:

preg_match_all('/(\?|&)([^=]+=[^&]*)/', $string , $matches);

then

print_r($matches[2]);
/*
Array
(
    [0] => url=var
    [1] => file_id=var
    [2] => test=var
)
*/

Hope it works 4 u.

A quick google for "PHP GET" gives this page from w3schools:

http://www.w3schools.com/php/php_get.asp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!