-1

How can i get all the $_GET name => values from a url using preg_match_all

Example url1: http://www.example.com?go1=test1

Example url2: http://www.example.com?go2=test2

Example url3: http://www.example.com?go1=test1&go3=test3

The return need to be via array if is possible

$array = array();
$array[name1] = value1;
$array[name2] = value2;

...

Matei Zoc
  • 3,341
  • 3
  • 14
  • 15

3 Answers3

1

How about something like:

$url = 'http://www.example.com?go1=test1&go3=test3';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $array);

This will put the query string parameters in $array.

George Brighton
  • 5,071
  • 9
  • 27
  • 36
0

This may help you to get variables from url, you can also use parse_url to get a single Url components like

Using parse_url:

$url='http://www.example.com?go1=test1&go3=test3';
var_dump(parse_url($url)['query']);

See Demo :http://codepad.viper-7.com/AVFDdy

Using preg_match_all:

<?php
$re = "/\\?(.*)/"; 
$str = "http://www.example.com?go1=test1"; 

preg_match_all($re, $str, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';

See Regex :https://regex101.com/r/eB0rQ2/1

Always Sunny
  • 32,751
  • 7
  • 52
  • 86
0

try to look $_SERVER['QUERY_STRING'] option,hopefully it will be helpful.