How to use parse_url function in PHP

小云云
Release: 2023-03-20 22:04:02
Original
2691 people have browsed it

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);
Copy after login

Result

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

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); 
?>
Copy after login

Result

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

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);
Copy after login

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.
Copy after login
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!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!