How can I grab an empty parameter name in PHP e.g. http://yoursite.com/?123 > "123" ?
I want to use this as URL shortener to redirect to the ID from the parameter.
How can I grab an empty parameter name in PHP e.g. http://yoursite.com/?123 > "123" ?
I want to use this as URL shortener to redirect to the ID from the parameter.
You have built-in function that will parse your URL parse_url()
and string query parser parse_str()
:
$url = 'http://yoursite.com/?123&a=b&abc=';
$parsed = parse_url($url);
parse_str($parsed['query'], $query);
var_dump($query);
Keys in array $query
that have empty string as value, are the keys you are looking for.
$filtered = array_filter($query, function($v) { return empty($v); });
#$filtered = array_keys($filtered);
print_r($filtered);
Use array_keys
to fetch all keys.
var_dump(array_keys($_GET));
array(1) { 0 => "123" }
"123"
will also appear if you iterate over $_GET
, like with foreach
.