How do I parse URL GET data having the URL stored

2020-05-02 05:59发布

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?

标签: php
4条回答
▲ chillily
2楼-- · 2020-05-02 06:06

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);
?>
查看更多
Explosion°爆炸
3楼-- · 2020-05-02 06:12

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

查看更多
可以哭但决不认输i
4楼-- · 2020-05-02 06:17

You can use parse_url().

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

For more details, refer php manual.

查看更多
男人必须洒脱
5楼-- · 2020-05-02 06:17

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'];
查看更多
登录 后发表回答