Get the GET variables from a URL String

2019-01-14 03:22发布

Hey, say I have a url just being passed through my php is there any easy way to get some GET variables that are being passed through it? It's not the actual url of the page or anything.

like a just have a string containing

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

Whats the best way to get the values for those variables?

5条回答
叛逆
2楼-- · 2019-01-14 03:52

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.

查看更多
Luminary・发光体
3楼-- · 2019-01-14 03:59

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

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

查看更多
我命由我不由天
4楼-- · 2019-01-14 04:00

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

$queryStr = $_SERVER['QUERY_STRING'];

查看更多
萌系小妹纸
5楼-- · 2019-01-14 04:04

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

查看更多
Root(大扎)
6楼-- · 2019-01-14 04:07
$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
) */
查看更多
登录 后发表回答