Home  >  Article  >  Backend Development  >  How to use parse_url function in PHP

How to use parse_url function in PHP

小云云
小云云Original
2018-03-08 13:31:182644browse

A useful function in PHP, parse_url, is especially convenient for analysis of information capture. This article mainly shares with you how to use the parse_url function in PHP, hoping to help everyone.

For example

Code

$url = "http://www.electrictoolbox.com/php-extract-domain-from-full-url/"; 
$parts = parse_url($url);

Result

Array ( 
[scheme] => http 
[host] => www.electrictoolbox.com 
[path] => /php-extract-domain-from-full-url/ 
)

Code

<?php $url = &#39;http://username:password@hostname/path?arg=value#anchor&#39;; 
print_r(parse_url($url)); 
echo parse_url($url, PHP_URL_PATH); 
?>

Result

Array ( 
[scheme] => http 
[host] => hostname 
[user] => username 
[pass] => password 
[path] => /path 
[query] => arg=value 
[fragment] => anchor 
)

You can see , it is easy to decompose the various parts of a URL, and it is also easy to extract the specified parts. For example,

 echo parse_url($url, PHP_URL_PATH);

is to set the following parameters in the second parameter:

 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.
Recommended related articles:
1.Explanation of using php pathinfo(), parse_url(), basename() functions to parse URLs
2.Introduction to php using parse_url and parse_str to parse URLs
Related video recommendations:
1.Dugu Jiujian (4)_PHP video tutorial

Related recommendations:

How to parse the parse_url parameter into an array parse_str

##10 recommended courses on parse_url()

php parse_url() function summary of URL usage

The above is the detailed content of How to use parse_url function in PHP. 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