Home  >  Article  >  Backend Development  >  Introduction to the usage of parse_url() function in php

Introduction to the usage of parse_url() function in php

PHP中文网
PHP中文网Original
2017-06-05 10:54:171211browse

This article introduces the usage of parse_url() function in PHP. Friends who need to use parse_url() function can refer to this article.

A useful function parse_url in PHP is particularly convenient for analysis of information capture. An example is as follows:

$url = "http://www.daimajiayuan.com/course/"; $parts = parse_url($url);

Code output:

Array
 (
  [scheme] => http 
  [host] => www.daimajiayuan.com 
  [path] => /course
)

Another example:

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

Code output:

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

As you can see, it can be easily decomposed into a For each part of the URL, it is easy to extract the specified part, such as and another example:

echo parse_url($url, PHP_URL_PATH);

In the second parameter, set the following parameters:
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.

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