This article demonstrates how to fetch the request parameters from a URL string in PHP.

1. Using parse_url() function

You can use the parse_url() function with the parse_str() function to fetch the request parameters from a URL string. The parse_url() function parses a URL string into an associative array of its components. The query string is the value associated with the 'query' key of the returned array. Then you can use the parse_str() function to parse the query string into variables for each of the parameters.

Download  Run Code

 
You can directly get the query string of the URL parsed by passing the predefined constant PHP_URL_QUERY to the second parameter of parse_url(). Note that now parse_url() returns a string.

Download  Run Code

 
If you need a specific query parameter, you can access array elements using the square bracket syntax.

Download  Run Code

2. Using preg_match() function

You may also use a regex to extract specific parameters from a URL string. In PHP, the preg_match() function is used for performing a regular expression match. Here’s an example of how you could achieve that.

Download  Run Code

3. Using $_GET function

Finally, you may use the $_GET predefined variables to get an associative array of variables passed to the current script via the URL parameters. For example, the following solution outputs "chrome" for the user-entered URL "https://www.google.com/?query=fifa&browser=chrome".

Download  Run Code

That’s all about fetching the request parameters from a URL string in PHP.