Sometimes, we want to get parameters from a URL string with PHP.
In this article, we’ll look at how to get parameters from a URL string with PHP.
How to get parameters from a URL string with PHP?
To get parameters from a URL string with PHP, we can use the parse_url
function.
For instance, we write
$parts = parse_url($url);
parse_str($parts["query"], $query);
echo $query["email"];
to call parse_url
with the $url
to parse the $url
string into an associative array.
Then we call parse_str
with $parts["query"]
and $query
to parse $parts["query"]
into the $query
associative array.
And then we get the query parameter’s value with key email
with $query["email"]
.
Conclusion
To get parameters from a URL string with PHP, we can use the parse_url
function.