简体   繁体   English

您如何使用php在网址中获取变量?

[英]How do you just get the vars in a url using php?

I have a url, 我有一个网址,

example.com/?/page1

and i want to find out what the GET part of the url is, for example: 我想找出网址的GET部分是什么,例如:

?/page1

how do i do this? 我该怎么做呢? like, without having to split strings and stuff? 喜欢,而不必拆分字符串和东西?

The following variable will contain the entirety of the query string (that is, the portion of the url following the ? character): 以下变量将包含整个查询字符串(即,?字符之后的url部分):

$_SERVER['QUERY_STRING']

If you're curious about the rest of the contents of the $_SERVER array, they're listed in the PHP manual here . 如果您对$ _SERVER数组的其余内容感到好奇,请在此处的PHP手册中列出它们。

That is a strange GET URL because the normal format is: 那是一个奇怪的GET URL,因为常规格式为:

domain.com/page.html?a=1&b=2

PHPinfo will help a whole lot: PHPinfo将提供很多帮助:

<?php phpinfo(); ?>

Output of relevance: 相关输出:

<?php
// imagine URL is 'domain.com/page.html?a=1&b=2'
phpinfo();
echo $_GET['a']; // echoes '1'
echo $_GET['b']; // echoes '2'
echo $_SERVER['QUERY_STRING']; // echoes 'a=1&b=2'
echo $_SERVER['REQUEST_URI']; // echoes '/path/to/page.php?a=1&b=2'
?>

Sounds to me like you want to look up parse_url() and parse_str() . 对我来说,听起来像您想查找parse_url()parse_str() Of course, these will 'split' the string(s) behind the scenes. 当然,这些将在幕后“分割”字符串。

You can then use http_build_query to rebuild the query, if required. 然后,可以根据需要使用http_build_query重建查询。


 $url = "example.com/?/page1";
 $res = parse_url($url, PHP_URL_QUERY);
 print "Query:".$res."\n";

Output is: 输出为:

Query:/page1
for($i = 0, $e = count($_GET[]); $i < $e; $i++) {
    echo $_GET[$i];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM