问题
I have a url,
example.com/?/page1
and i want to find out what the GET part of the url is, for example:
?/page1
how do i do this? like, without having to split strings and stuff?
回答1:
The following variable will contain the entirety of the query string (that is, the portion of the url following the ? character):
$_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.
回答2:
That is a strange GET URL because the normal format is:
domain.com/page.html?a=1&b=2
PHPinfo will help a whole lot:
<?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'
?>
回答3:
Sounds to me like you want to look up parse_url() and 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.
$url = "example.com/?/page1";
$res = parse_url($url, PHP_URL_QUERY);
print "Query:".$res."\n";
Output is:
Query:/page1
回答4:
for($i = 0, $e = count($_GET[]); $i < $e; $i++) {
echo $_GET[$i];
}
来源:https://stackoverflow.com/questions/1280682/how-do-you-just-get-the-vars-in-a-url-using-php