0

I have an url stored in a variable such as:

$url = 'example.com?a=20&i=10'

How do I get values stored in variable a and variable i? Is there a short way to do this?

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410
  • 1
    possible duplicate of [how do i parse url php](http://stackoverflow.com/questions/5624370/how-do-i-parse-url-php) – deceze Jul 10 '12 at 07:43
  • http://stackoverflow.com/questions/11245963/how-to-retrieve-complete-url-from-address-bar-using-php/11246089#comment14778209_11246089 Check my answer (second answer) – dpitkevics Jul 10 '12 at 07:44

4 Answers4

2

You can use parse_url().

data = parse_url($url)
print_r($data['query'])

For more details, refer php manual.

Jishnu
  • 175
  • 1
  • 7
2

You could use parse_url and parse_str:

<?php
  $url = 'example.com?a=20&i=10';
  $tmp=parse_url($url);
  parse_str($tmp['query'],$out);
  print_r($out);
?>

Demo

stewe
  • 41,820
  • 13
  • 79
  • 75
0

Try this:

<?php
  $url = 'example.com?a=20&i=10';
  $result = parse_url($url);
  parse_str($result['query'],$getVar);
  echo 'value of a='. $getVar['a'];
  echo 'value of i='. $getVar['i'];
Vimalnath
  • 6,373
  • 2
  • 26
  • 47
  • Have a look at: http://meta.stackexchange.com/questions/27534/see-who-is-upvoting-downvoting-my-question-answer – stewe Jul 11 '12 at 06:50
0

If you want to access those variables, check out the extract() function, it will create variables $a and $i from the parse_str() function above.

However, it will overwrite any existing variable called $a so its to be used with caution.

<?php
  $url = 'example.com?a=20&i=10';
  $tmp=parse_url($url);
  parse_str($tmp['query'],$out);
  extract($out);
?>
Chris
  • 546
  • 1
  • 4
  • 18