2

I need to know how to get the $_GET parameter from requested url. If I use for example

$url = $_SERVER['REQUEST_URI'];
$parse_url = parse_url($url, PHP_URL_QUERY);

echo $parse_url; will output query=1, but I need only the parameter not parameter AND value to check if parameter is in array.

4 Answers4

2

Based on your example, simply exploding the = might quickly suits your need.

$url = $_SERVER['REQUEST_URI'];
$parse_url = parse_url($url, PHP_URL_QUERY);
$queryparam = explode("=", $parse_url);
echo $queryparam[0];
/* OUTPUT query */

if (in_array($queryparam[0], $array_of_params)){ ... }

But you can simply achieve the same thing like this:

if (@$_GET["query"] != ""){ ... }
NVRM
  • 11,480
  • 1
  • 88
  • 87
  • Is it possible to use wildcards for explode? I have queries like filter[2]=, so "=" doesn't work and must use "%" instead. – Serpentdriver Dec 14 '19 at 11:30
  • @NVRAM why use ``$_SERVER['REQUEST_URI']`` and then use ``parse_url``, better use ``$_SERVER['QUERY_STRING']`` to just return the query params ?? – OMi Shah Dec 14 '19 at 11:33
  • Can you paste the complete url params, example `?query=3&filter=some%20stuffs` You can explode by `&` then explode again for example. Some others answers can help also for this. @OMi Shah Yes absolutely! There is many ways,others answers are correct too. – NVRM Dec 14 '19 at 11:35
  • Solution from @NVRAM is okay. I need only the first parameter. $_SERVER['QUERY_STRING'] will output all queries. – Serpentdriver Dec 14 '19 at 11:36
  • 1
    This one of multiple examples: filter[2]=&filter[3]=27. I need only the first parameter. Buth there are also queries available with simple query like query=1 without [] in parameter. But need solution for both. – Serpentdriver Dec 14 '19 at 11:39
  • 1
    hmm okay then :) – OMi Shah Dec 14 '19 at 11:41
  • `echo preg_replace("/\[.\]/", "",'filter[6]');` output `filter`. But won't throw an error if there is no brackets^ – NVRM Dec 14 '19 at 11:50
  • I already tried it with preg_replace, but my knowledge about using patterns wasn' t good enough, so it will take a little bit time to check your solution. – Serpentdriver Dec 14 '19 at 11:54
  • No worries, learning them now! You can achieve the same with just explode in chain (and other ways too!). Good luck! – NVRM Dec 14 '19 at 12:00
  • I've just checked your last solution with preg_replace, but doesn't work. If I use `$lc_request_uri = $_SERVER['REQUEST_URI']; $parsed_url = parse_url($lc_request_uri, PHP_URL_QUERY); echo preg_replace("/\[.\]/", "",$parsed_url);` Output for echo: filter%5B2%5D=22 for Query: filter[2]=22 – Serpentdriver Dec 14 '19 at 13:53
  • `echo preg_replace("/\[.*/", "", urldecode("filter[2]=22"));` output `filter` – NVRM Dec 14 '19 at 14:37
  • 1
    I already fixed it `$lc_request_uri = $_SERVER['REQUEST_URI']; $parsed_url = parse_url($lc_request_uri, PHP_URL_QUERY); $url_replace = preg_replace("/\%5B.\%5D/", "", $parsed_url); if (isset($url_replace)) { $cleanquery = explode("=", $url_replace); } else { $cleanquery = explode("=", $parsed_url); }` – Serpentdriver Dec 14 '19 at 15:22
1

Something like:

// Example URL: http://somewhere.org?id=42&name=bob

// An array to store the parameters
$params = array();
parse_str($_SERVER['QUERY_STRING'], $params);

// Access what you need like this:
echo 'ID: ' .$params['id']; 
Stuart
  • 6,630
  • 2
  • 24
  • 40
  • Again and forget what I need the $_GET parameter for. I want to parse a URL to get only the parameter name and not the parameter && value. If query_string is query=1 I need "query". – Serpentdriver Dec 14 '19 at 11:09
0

The Arry $_GET contains all needed informations. $ _SERVER ['REQUEST_URI'] and parse_url not needed. As an example I have the following browser line:

http://localhost/php/test.php?par&query=1

Let's see what $ _GET contains

echo '<pre>';
var_export($_GET);

Output

array (
  'par' => '',
  'query' => '1',
) 

With array_keys() I get all keys.

$keys = array_keys($_GET);
var_export($keys);

Output:

array (
  0 => 'par',
  1 => 'query',
) 

The first key I get with (see also comment from @bobble bubble)

echo $keys[0];  //par

or

echo key($_GET);

With array_key_exists () I can check whether a key is available.

if(array_key_exists('query',$_GET)){
  echo 'query exists';
}
jspit
  • 7,276
  • 1
  • 9
  • 17
-1

You can use basename() too, it Returns trailing name component of path. For example:

$lastPart=explode("=",basename('http://yourdomain.com/path/query=1'));
echo $lastPart[0];

Output

query
A. Cedano
  • 557
  • 7
  • 39