The parse_url() function is an inbuilt functionin PHP which 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 )
Parameters: This function accepts two parameters as mentioned above and described below:
- url: This parameter holds the URL to be parsed. The invalid characters are replaced by _ (underscore).
- component: This parameter specifies any of the component ( PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT ) to retrieve a specific URL in the form of string.
Return Values:
- It return an associative array if the component parameter is omitted.
- It return a string if the component parameter is specified.
- It return false, if the parameter is malformed URL.
Below examples illustrate the use of parse_url() function in PHP:
Example 1:
<?php // Declare a variable and initialize it with URL // Use parse_url() function to parse the URL var_dump(parse_url($url)); var_dump(parse_url($url, PHP_URL_SCHEME)); ?> |
array(4) {
["scheme"]=>
string(4) "http"
["host"]=>
string(17) "geeksforgeeks.org"
["path"]=>
string(5) "/php/"
["fragment"]=>
string(6) "basics"
}
string(4) "http"
Example 2:
<?php // Declare a variable and initialize it with URL $url = '//www.geeksforgeeks.org/path?php=PHP'; // Use parse_url() function to // parse the URL var_dump(parse_url($url)); ?> |
array(3) {
["host"]=>
string(21) "www.geeksforgeeks.org"
["path"]=>
string(5) "/path"
["query"]=>
string(7) "php=PHP"
}
Reference: http://php.net/manual/en/function.parse-url.php
Recommended Posts:
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | fpassthru( ) Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
- PHP | SimpleXMLElement children() Function
- PHP | array_intersect_ukey() Function
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.

