Example of four URL parsing methods in PHP

Source: Internet
Author: User
When URL parameters are known, we can use $ _ GET to obtain the relevant parameter information ($ _ GET [\\\ name \\]) According to our own situation. how can we get the parameter information on the URL in an unknown situation? First, use the $ _ SERVER built-in array variable.

When URL parameters are known, we can use $ _ GET to obtain the relevant parameter information ($ _ GET [\ 'name \ ']) based on our own situation. how can we get the parameter information on the URL in an unknown situation?

First: use the $ _ SERVER built-in array variable

Obtain the URL parameter from the original $ _ SERVER ['query _ string']. Generally, this variable returns data similar to this: name = tank & sex = 1.

To include a file name, you can use $ _ SERVER ["REQUEST_URI"] (The Returned result is similar to:/index. php? Name = tank & sex = 1)

Second: use pathinfo built-in functions

  1. $ Test = pathinfo ("http: // localhost/index. php ");
  2. Print_r ($ test );
  3. /*
  4. The result is as follows:
  5. Array
  6. (
  7. [Dirname] => http: // localhost // url path
  8. [Basename] => index. php // Complete file name
  9. [Extension] => php // file name suffix
  10. [Filename] => index // file name
  11. )
  12. */
  13. ?>

Third: use the parse_url built-in function

  1. $ Test = parse_url ("http: // localhost/index. php? Name = tank & sex = 1 # top ");
  2. Print_r ($ test );
  3. /*
  4. The result is as follows:
  5. Array
  6. (
  7. [Scheme] => http // what protocol is used
  8. [Host] => localhost // host name
  9. [Path] =>/index. php // path
  10. [Query] => name = tank & sex = 1 // The passed parameter
  11. [Fragment] => top // The anchor behind the root
  12. )
  13. */
  14. ?>

Type 4: Use the basename built-in function

  1. $ Test = basename ("http: // localhost/index. php? Name = tank & sex = 1 # top ");
  2. Echo $ test;
  3. /*
  4. The result is as follows:
  5. Index. php? Name = tank & sex = 1 # top
  6. */
  7. ?>

In addition, you can use the regular expression matching method to obtain the required value. this method is more precise and the efficiency is not considered for the moment. below we will expand the regular expression processing method in practice:

  1. Preg_match_all ("/(\ w + = \ w +) (# \ w + )? /I "," http: // localhost/index. php? Name = tank & sex = 1 # top ", $ match );
  2. Print_r ($ match );
  3. /*
  4. The result is as follows:
  5. Array
  6. (
  7. [0] => Array
  8. (
  9. [0] => name = tank
  10. [1] => sex = 1 # top
  11. )
  12. [1] => Array
  13. (
  14. [0] => name = tank
  15. [1] => sex = 1
  16. )
  17. [2] => Array
  18. (
  19. [0] =>
  20. [1] => # top
  21. )
  22. )
  23. */
  24. ?>

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.