PHP Function details: parse_url ()

Source: Internet
Author: User
PHP Function details: parse_url ()

The parse_url () function can parse the URL and return its components. Its usage is as follows:

Array parse_url (string url)

This function returns an associated array that contains various components of an existing URL. If one of them is missing, an array item is not created for this component. Components:

  • Scheme-such as http
  • Host
  • Port
  • User
  • Pass
  • Path
  • Query-question mark? After
  • Fragment-after the hash symbol #

This function does not mean that the given URL is valid. it only separates the parts in the list above. Parse_url () can accept incomplete URLs and resolve them correctly as much as possible. This function does not work for URLs with relative paths.

  1. $ Url = "http://www.nowamagic.net/welcome ";
  2. $ Parts = parse_url ($ url );

  3. Print_r ($ parts );
  4. ?>


The program running result is as follows:

  1. Array ([scheme] => http [host] => www.nowamagic.net [path] =>/welcome /)

  2. $ Url = 'http: // username: password @ hostname/path? Arg = value # anchor ';
  3. Print_r (parse_url ($ url ));
  4. Echo parse_url ($ url, PHP_URL_PATH );
  5. ?>


Program output:

  1. Array
  2. (
  3. [Scheme] => http
  4. [Host] => hostname
  5. [User] => username
  6. [Pass] => password
  7. [Path] =>/path
  8. [Query] => arg = value
  9. [Fragment] => anchor
  10. )
  11. /Path


As you can see, it is easy to break down all parts of a URL. it is also easy to take the specified part, for example:

  1. 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


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.