How to get parameters from a URL string in PHP?
The parameters from a URL string can be be retrieved in PHP using pase_url() and parse_str() functions.
Note: Page URL and the parameters are separated by the ? character.
parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it. It parse an URL and return an associative array which contains its various components.
Syntax:
parse_url( $url, $component = -1 )
parse_str() Function: The parse_str() function is used to parse a query string into variables. The string passed to this function for parsing is in the format of a query string passed via a URL.
Syntax:
parse_str( $string, $array )
Approach: Parse the URL string using parse_url() function which will return an associative array that contains its (passed URL) various components. The query of the array returned by parse_url() function which contains a query string of URL.
Below examples uses parse_url() and parse_str() function to get the parameters from URL string.
Example 1:
<?php // Inintialize URL to the variable // Use parse_url() function to parse the URL // and return an associative array which // contains its various components $url_components = parse_url($url); // Use parse_str() function to parse the // string passed via URL parse_str($url_components['query'], $params); // Display result echo ' Hi '.$params['name']; ?> |
Hi Tonny
Example 2:
<?php // Inintialize URL to the variable // Use parse_url() function to parse the URL // and return an associative array which // contains its various components $url_components = parse_url($url); // Use parse_str() function to parse the // string passed via URL parse_str($url_components['query'], $params); // Display result echo ' Hi '.$params['name'].' your emailID is '.$params['email']; ?> |
Hi Amit your emailID is amit1998@gmail.com
Recommended Posts:
- JavaScript | Know the value of GET parameters from URL
- How to get URL Parameters using JavaScript ?
- JavaScript | Function Parameters
- How to Create Query Parameters in JavaScript ?
- Perl | Passing Complex Parameters to a Subroutine
- JavaScript | Passing parameters to a callback function
- How to declare the optional function parameters in JavaScript ?
- How to serialize an object into a list of URL query parameters using JavaScript ?
- How to check a string starts/ends with a specific string in jQuery ?
- JavaScript | Check if a string is a valid JSON string
- JavaScript | Difference between String.slice and String.substring
- How to count string occurrence in string using JavaScript?
- PHP | Program to check a string is a rotation of another string
- What is the difference between 'String' and 'string' in TypeScript ?
- ES6 | String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

