-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,739
  • 3
  • 16
  • 18

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,131
  • 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

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

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