Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return the names of $_GET variables in a url php

Tags:

url

php

get

I want to save the name of all the $_GET variables in a url, but im not sure where to start, or finish for that matter.

For example:

if i have:

url: test.com/forums.php?topic=blog&discussion_id=12

can i use php to get the name, i.e. "topic" and "discussion_id from the $_GET variables and can i then store the values: "topic" and "discussion_id" in an array?

like image 207
Jai Avatar asked May 18 '11 13:05

Jai


2 Answers

You can get this by calling array_keys on $_GET:

$getVars = array_keys($_GET);
like image 95
lonesomeday Avatar answered Sep 30 '22 05:09

lonesomeday


If this isn't about the current URL, but just some $url string you want to extract the parameters from then:

parse_str(parse_url($url, PHP_URL_QUERY), $params);

will populate $params with:

[topic] => blog
[discussion_id] => 12
like image 24
mario Avatar answered Sep 30 '22 04:09

mario