Home > Backend Development > PHP Tutorial > Implementation code for using parse_url() to parse URLs in PHP (detailed explanation of parse_url)_PHP tutorial

Implementation code for using parse_url() to parse URLs in PHP (detailed explanation of parse_url)_PHP tutorial

WBOY
Release: 2016-07-21 15:21:58
Original
1029 people have browsed it

PHP parsing URL function: parse_url detailed explanation
parse_url — parses the URL and returns its components

Description

array parse_url ( string $url )
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 to break it down into the parts listed below. Incomplete URLs are also accepted and parse_url() will try to parse them as correctly as possible.
Parameters
url
The URL to be parsed
Return value
For severely unqualified URLs, parse_url() may return FALSE and issue E_WARNING. Otherwise an associative array is returned with (at least one) components:
scheme – like http
host
port
user
pass
path
query – in Question mark? After
fragment – ​​after hash symbol # >

$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 will send a large list of URLs, and then if we want to parse them, we can only continue to cut tokens to parse them. But in PHP, you must learn to use functions written by predecessors, so that the development speed will be faster than others. Of course, if you want to parse a URL, you have to find relevant instructions. What I want to introduce today is the parse_url function in PHP.

This function is mainly used to parse URLs. First, let’s take a look at the prototype of this function. Its official description is as follows:


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

The PHP official gave an example, and I expanded it as follows to facilitate explanation.



Copy code

The code is as follows:


$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 ResultArray (
[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, and host is the website URL, 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 "$component" part, 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 represent Array scheme, host, user, pass, path, query and fragment.

Similarly, for example, if you use PHP_URL_PATH and PHP_URL_QUERY, the following is his demonstration.



Copy code

The code is as follows:


$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"); ?> And the output will be as follows: /path
arg1=value1&arg2=value2



http://www.bkjia.com/PHPjc/324790.html
www.bkjia.com

true
http: //www.bkjia.com/PHPjc/324790.html

TechArticlePHP parsing URL function: parse_url detailed explanation parse_url — parses the URL and returns its component description array parse_url ( string $url ) This function parses a URL and returns an associative array,...
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template