Jai
Jai

Reputation: 2204

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

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?

Upvotes: 5

Views: 10846

Answers (5)

LoneWOLFs
LoneWOLFs

Reputation: 2306

Use the following code to grab data from the URL using GET. Change it to $_POST will work for post.

<?php
foreach ( $_GET as $key => $value ) 
{
        //other code go here
    echo 'Index : ' . $key . ' & Value : ' . $value;
    echo '<br/>';
}
?>

Upvotes: 3

James
James

Reputation: 13501

It's an array:

print_r($_GET);

Fetch the elements as you would with any other array.

Upvotes: 0

mario
mario

Reputation: 145482

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

Upvotes: 5

heximal
heximal

Reputation: 10517

$_GET is usual php-array. you may use it in foreach loop:

foreach ($_GET as $k => $v)
  echo ($k . '=' . $v);

Upvotes: 1

lonesomeday
lonesomeday

Reputation: 237865

You can get this by calling array_keys on $_GET:

$getVars = array_keys($_GET);

Upvotes: 13

Related Questions