php 从 URL 字符串中获取 GET 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3136425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the GET variables from a URL String
提问by Belgin Fish
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.
嘿,说我有一个 url 刚刚通过我的 php 有没有什么简单的方法来获取一些正在通过它的 GET 变量?它不是页面的实际 url 或任何东西。
like a just have a string containing
就像一个只有一个包含的字符串
http://www.somesite.com/index.php?url=var&file_id=var&test=var
http://www.somesite.com/index.php?url=var&file_id=var&test=var
Whats the best way to get the values for those variables?
获取这些变量的值的最佳方法是什么?
回答by cristis
回答by Kris
$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
) */
回答by Belgin Fish
It's actually a lot easier than writing any custom functions.
它实际上比编写任何自定义函数要容易得多。
$queryStr = $_SERVER['QUERY_STRING'];
$queryStr = $_SERVER['QUERY_STRING'];
回答by misterte
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.
希望它适用于 4 u。
回答by Anon.
A quick google for "PHP GET" gives this page from w3schools:
一个快速的“PHP GET”谷歌从 w3schools 给出了这个页面:

