Reputation: 3256
I've this url: http://localhost/alignment/?page_id=52&q=2 I want to get the portion: ?page_id=52 , how can I do that?
Upvotes: 0
Views: 497
Reputation: 29925
$_SERVER['QUERY_STRING']
or
$url = "http://localhost/alignment/?page_id=52&q=2";
$bits = explode("?", $url);
$querystring = $bits[1]; // this is what you want
but the first one will be much more reliable and is easier. :)
EDIT
if you meant that you just wanted that one variable use:
$_GET['page_id']
Upvotes: 7
Reputation: 1133
Assign the url to string and then explode() it, http://php.net/manual/en/function.explode.php, using the ? as a delimiter
Upvotes: 0
Reputation: 17472
If you have it as a string, use parse_url
. Documentation here. Get the query
value of it. or if its current request use $_SERVER['QUERY_STRING']
Upvotes: 0
Reputation: 55334
This is called a query string. The main portion of the query string is separated by the rest of the URL with a ?
. Each argument in the query string is separated by a &
.
PHP:
$_SERVER['QUERY_STRING'];
If you want to get the individual pieces, use:
$_GET['page_id']; //etc...
Upvotes: 2
Reputation: 44093
You can get the whole query string with $_SERVER['QUERY_STRING']
, but you would have to parse out page_id part. If you insist on doing things manually the function parse_str may come in handy.
A better choice would be to just use the predefined $_GET global variable. $_GET['page_id']
would give you value 52.
Upvotes: 1