Home > Article > Backend Development > Detailed explanation of the definition and usage of php parse_url() function
What is the function of php parse_url() function?
The parse_url() function in php parses the URL and returns the components of the URL. Its syntax is as follows:
Syntax
array parse_url(string $url,int $component)
Parameter details
Parameter name | Parameter description |
$url | The URL to be parsed, invalid characters will be replaced with _. |
Specify one of 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 get the string of the specified part of the URL. (Except when specified as PHP_URL_PORT, an integer value will be returned). |
ps : This function parses a URL and returns an associative array containing the various components that appear in the URL.
This function is not used to verify the validity of the given URL, but only to break it into the parts listed below. Incomplete URLs are also accepted, and parse_url() will try to parse them as correctly as possible.Return value
<?php
$url = "http://www.php.cn:8080/index.php?name=wxp&id=2";
$parts = parse_url($url);
print_r($parts);
?>
Code running result:
Let’s look at a more complicated example:
<?php $url = 'http://hello:manong@jiaochen/blog?name=wxp#student'; print_r(parse_url($url)); echo "<br/><br/>"; echo parse_url($url, PHP_URL_PATH); ?>
Code running result:
As can be seen from this code, we can set the second parameter to obtain each part of the URL . The value of the second parameter can be:
1.
Detailed explanation of php The pathinfo() function obtains the file path informationDetailed explanation of the usage of the php basename() function to obtain the file name Example explanation of using php pathinfo(), parse_url(), basename() functions to parse URLsThe above is the detailed content of Detailed explanation of the definition and usage of php parse_url() function. For more information, please follow other related articles on the PHP Chinese website!