Home  >  Article  >  Backend Development  >  Usage of function parse_url in PHP

Usage of function parse_url in PHP

小云云
小云云Original
2018-02-24 14:41:0525495browse

This article mainly shares with you a useful function parse_url in PHP, which is especially convenient for analysis of information capture. I hope it can help everyone.

Recommended Manual:php Complete Self-Study Manual

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 
)

As you can see, it is easy to decompose the various parts of a URL, and it is also easy to extract the specified parts, such as

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.Use php pathinfo (), parse_url(), basename() function parsing URL examples
2.Introduction to php using parse_url and parse_str to parse URLs
Related video recommendations:
1.Dugu Jiujian (4)_PHP video tutorial

Other recommendations:

How to parse parse_url parameter into array parse_str ​ ​
10 recommended courses on parse_url()
The difference between the php parse_str() function and the parse_url() function in parsing URLs

The above is the detailed content of Usage of function parse_url 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