How can I get parameters from a URL string?
You can use the parse_url()
and parse_str()
for that.
$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['email'];
If you want to get the $url
dynamically with PHP, take a look at this question:
Get the full URL in PHP
How do I extract query parameters from a URL string in PHP?
You can use parse_url
and parse_str
like this:
$query = parse_url('http://www.example.com?test=123&random=abc', PHP_URL_QUERY);
parse_str($query, $params);
$test = $params['test'];
parse_url
allows to split an URL in different parts (scheme, host, path, query, etc); here we use it to get only the query (test=123&random=abc
). Then we can parse the query with parse_str
.
Strip off URL parameter with PHP
The safest "correct" method would be:
- Parse the url into an array with
parse_url()
- Extract the query portion, decompose that into an array using
parse_str()
- Delete the query parameters you want by
unset()
them from the array - Rebuild the original url using
http_build_query()
Quick and dirty is to use a string search/replace and/or regex to kill off the value.
Get URL query string parameters
$_SERVER['QUERY_STRING']
contains the data that you are looking for.
DOCUMENTATION
- php.net: $_SERVER - Manual
PHP - Get indexed URL query string parameters from URL
Try something like this
<?php
$param_names = [
'amount',
'category',
'itemsku',
'quantity',
];
$data = [];
foreach ($_GET as $key => $val) {
foreach ($param_names as $param_name) {
if (strpos($key, $param_name) === 0) {
$idx = substr($key, strlen($param_name), 1);
$data[$idx][$param_name] = $val;
}
}
}
var_dump($data);
This is the result
array (size=2)
1 =>
array (size=3)
'amount' => string '233.55' (length=6)
'category' => string 'clothing' (length=8)
'itemsku' => string '01235654' (length=8)
'quantity' => string '1' (length=1)
2 =>
array (size=4)
'amount' => string '156.99' (length=6)
'category' => string 'accessories' (length=11)
'itemsku' => string '525124' (length=6)
'quantity' => string '3' (length=1)
PHP - How to get $_GET Parameter without value
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"] != ""){ ... }
Related Topics
How to Redirect Stdout to a File in PHP
Post Content-Length Exceeds the Limit
Why Should I Use Templating System in PHP
Email from PHP Has Broken Subject Header Encoding
Correct Way to Set Bearer Token with Curl
How to Efficiently Find the Closest Locations Nearby a Given Location
How to Install the Ext-Curl Extension with PHP 7
How to Easily Consume a Web Service from PHP
PHP Objects VS Arrays -- Performance Comparison While Iterating
How to Get Email Address from a Long String
Example of How to Use Fastcgi_Finish_Request()