30

In PHP, I need a function to convert a querystring from an URL, say: http://example.com?key1=value1&key2=value2 into a PHP associative array : array ['key1' => 'value1', 'key2' => 'value2'].

I've come up to this piece of code. It works, but I find it a bit lengthy. (And PHP has built-in functions for everything: I'm surprised I haven't found anything out-of-the-box, something like the reverse of http_build_query.)

Can you suggest a better way to do this?

function getUrlParams($url) {
  $querystring = parse_url($url, PHP_URL_QUERY);
  $a = explode("&", $querystring);
  if (!(count($a) == 1 && $a[0] == "")) {
    foreach ($a as $key => $value) {
      $b = explode("=", $value);
      $a[$b[0]] = $b[1];
      unset ($a[$key]);
    }
    return $a;
  } else {
    return false;
  }
}
  • 1
    Possible duplicate of [Parse query string into an array](http://stackoverflow.com/questions/5397726/parse-query-string-into-an-array) – Simon East Oct 20 '16 at 03:19

5 Answers5

63

You can get just the atributes from a URL using parse_url()

Once you have that you can use parse_str() to convert them to variables, it works with multidimensional arrays too!

$str = "first=value&arr[]=foo+bar&arr[]=baz";

parse_str($str, $output);
echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
472084
  • 17,666
  • 10
  • 63
  • 81
  • 2
    `parse_str` ? Function name not explicit at all, but that's it! – Nicolas Le Thierry d'Ennequin Nov 30 '11 at 10:54
  • 3
    `parse_str` is not always the ideal solution because querystring parameters that contain a dot like `user.name` will be converted to `user_name`, since PHP doesn't support variable names that include a dot – Mark Jan 09 '14 at 02:39
  • 1
    It just adds the variables to your scope? That seems like an unnecessary security issue – A F Nov 16 '14 at 00:21
3

If you mean as what you written then it is very simple and don't need anything else there is a predefined Superglobal variable $_GET in PHP which itself represents all the query string as key, value pairs associative array.

Example:

// current page URI: http://localhost/test.php?key1=value1&key2=value2

echo '<pre>';
print_r($_GET);
echo '</pre>';

Result:

Array(
    [key1] = value1
    [key2] = value2
)

For more information about $_GET PHP superglobal goto: http://php.net/manual/en/reserved.variables.get.php

MNR
  • 727
  • 1
  • 9
  • 23
0
$url = 'http://example.com?key1=value1&key2=value2&key3=value3';

preg_match_all('/\w+=.*/',$url,$matches);

parse_str($matches[0][0], $output);

print_r($output);
Louis van Tonder
  • 3,664
  • 3
  • 31
  • 62
-2
foreach ($_GET as $key => $value) $arr["$key"]= $value;
Botz3000
  • 39,020
  • 8
  • 103
  • 127
lup
  • 5
  • 1
  • 6
    When providing code that solves the problem, it is best to also give at least a short explanation of how it works so that folks reading won't have to mentally parse it line by line to understand the differences. – Fluffeh Sep 28 '12 at 10:13
  • 7
    What does this accomplish that `$arr = $_GET` doesn't? – Dan Lugg Apr 07 '14 at 16:20
-3

Try using parse_url

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
k102
  • 7,861
  • 7
  • 49
  • 69
  • 2
    `parse_url` isn't making a array from a querystring. It just parses the URL and returns the different parts as strings. – Tim Aug 27 '13 at 15:08
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – pb2q Aug 27 '13 at 15:16
  • @pb2q no, this does not answer the question. – ahnbizcad Oct 28 '16 at 22:32