PHP parseURL a useful function

Source: Internet
Author: User
PHP parseURL a useful function

parse_url-resolves the URL and returns its constituent parts. Description Array parse_url (string $url)

This function resolves a URL and returns an associative array containing the various components that appear in the URL.

This function is not used to verify the legality of a given URL, but to decompose it into the sections listed below. An incomplete URL is also accepted, and Parse_url () tries to parse it as correctly as possible. return value

For severely unqualified URLs, parse_url () may return FALSE and emit e_warning. Otherwise, an associative array is returned with a component (at least one): scheme-such as HTTP host port user Pass path query-at the question mark? After fragment-after the hash symbol #

Examples are as follows:

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


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

Another example:

$url = ' Http://username:password@hostname/path?arg=value#anchor '; 
Print_r (Parse_url ($url)); 
Echo Parse_url ($url, php_url_path); 


Output: 
Array 
( 
[scheme] => HTTP 
[host] => hostname 
[user] => username 
[Pass] = > Password 
[path] =>/path 
[query] => arg=value 
[fragment] => anchor 

As you can see, it's easy to break down the various parts of a URL, so it's easy to get the specified parts out, like
Echo Parse_url ($url, Php_url_path);
In the second argument, you set the following parameters:
Php_url

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.