Home  >  Article  >  Backend Development  >  Detailed explanation of the definition and usage of php parse_url() function

Detailed explanation of the definition and usage of php parse_url() function

怪我咯
怪我咯Original
2017-05-26 11:48:042591browse

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

##$component 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).
Parameter name Parameter description
$url The URL to be parsed, invalid characters will be replaced with _.

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

For seriously unqualified URLs, parse_url() may return FALSE.

If the component parameter is omitted, an associative array array will be returned, and there will currently be at least one element in the array. The possible keys in the array are:

  • scheme - such as http

  • host

  • port

  • user

  • pass

  • ##path
  • query - after the question mark ?
  • fragment - after the hash symbol
  • #If the component parameter is specified, parse_url() Returns a string (or an integer when specified as PHP_URL_PORT ) instead of an array. If the specified component in the URL does not exist, NULL will be returned.

Example

<?php
$url = "http://www.php.cn:8080/index.php?name=wxp&id=2";
$parts = parse_url($url);
print_r($parts);
?>
Code running result:

Detailed explanation of the definition and usage of php parse_url() function Let’s look at a more complicated example:

<?php
$url = &#39;http://hello:manong@jiaochen/blog?name=wxp#student&#39;;
print_r(parse_url($url));
echo "<br/><br/>";
echo parse_url($url, PHP_URL_PATH);
?>

Code running result:

Detailed explanation of the definition and usage of php parse_url() functionAs 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:

    PHP_URL_SCHEME
  • PHP_URL_HOST
  • PHP_URL_PORT
  • PHP_URL_USER
  • PHP_URL_PASS
  • PHP_URL_PATH
  • PHP_URL_QUERY
  • PHP_URL_FRAGMENT
[Recommended related articles]:

1.

Detailed explanation of php The pathinfo() function obtains the file path information


2.

Detailed explanation of the usage of the php basename() function to obtain the file name

3.

Example explanation of using php pathinfo(), parse_url(), basename() functions to parse URLs

The 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn