php USES parse_url of to parse the url implementation code of parse_url

  • 2020-05-10 17:50:38
  • OfStack

PHP parses the URL function: parse_url
parse_url - parses URL and returns its constituent parts

instructions

array parse_url ( string $url )
This function parses an URL and returns an associative array containing the various components that appear in URL.
This function is not used to validate the validity of a given URL, but to break it down into the parts listed below. Incomplete URL is also accepted, and parse_url() tries to parse it as correctly as possible.
parameter
url
To parse URL
The return value
For severely unqualified URL, parse_url() may return FALSE and issue E_WARNING. Otherwise, an associative array is returned with the following components (at least 1) :
scheme � as http
host
port
user
pass
path
query in question mark? after
The fragment wok is after the hash symbol #
sample
parse_url () example
 
<?php 
$url = 'http://username:password@hostname/path?arg=value#anchor'; 
print_r(parse_url($url)); 
?> 

The above routine will output:
Array
(
[scheme] = > http
[host] = > hostname
[user] = > username
[pass] = > password
[path] = > /path
[query] = > arg=value
[fragment] = > anchor
)

Often we send a bunch of urls, and then we have to go through token all the time. However, in php, 1 must learn to use functions written by predecessors, so as to develop faster than others. Of course, if you want to parse a web address, you have to look for the relevant instructions, and today's example is the parse_url function in php.

This function is mainly used to parse web addresses, but first take a look at the prototype of this function. The official description is as follows:


mixed parse_url ( string $url [, int $component= -1 ] )

The php official gave an example, which I expanded as follows to facilitate the explanation.
 
<?php 
$url = 'http://username:password@hostname/path?arg1=value1&arg2=value2#anchor'; 
print_r(parse_url($url)); 
?> 

First, take a look at the output of this program
Array (
[scheme] = > http
[host] = > hostname
[user] = > username
[pass] = > password
[path] = > /path
[query] = > arg1=value1&arg2=value2
[fragment] = > anchor
)
Among them, scheme refers to the route he takes, host is the website of the platform, user is the user name, pass is the password, path is the path, query is the parameter, and fragment is the anchor point.

In the section of "$component", the following parameters can be passed.

PHP_URL_SCHEME
PHP_URL_HOST
PHP_URL_USER
PHP_URL_PASS
PHP_URL_PATH
PHP_URL_QUERY
PHP_URL_FRAGMENT
These parameters respectively represent scheme, host, user, pass, path, query and fragment in Array.

Similarly, for example, if PHP_URL_PATH and PHP_URL_QUERY are used, here is his demonstration.
 
<?php 
$url = 'http://username:password@hostname/path?arg1=value1&arg2=value2#anchor'; echo nl2br(parse_url($url, PHP_URL_PATH)."\n"); 
echo nl2br(parse_url($url, PHP_URL_QUERY)."\n"); 
?> 

The output will be as follows:
/path
arg1=value1&arg2=value2

Related articles: