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

半世苍凉 提交于 2019-12-11 10:51:58

问题


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

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