php How to get URL instance detailed

Source: Internet
Author: User
In the case of known URL parameters, we can use $_get to obtain the corresponding parameter information ($_get[' name ') according to our own situation; how do I get the parameter information on the URL in an unknown situation?

The first, using $_server built-in array variables

Relatively primitive $_server[' query_string ' to get, URL parameters, usually returned using this variable will be similar to the data: name=tank&sex=1
If you need to include a file name, you can use $_server["Request_uri"] (return similar to:/index.php?name=tank&sex=1)

The second, using pathinfo built-in functions

<?php$test = PathInfo ("http://localhost/index.php");p Rint_r ($test);/* The result is the following array (     [dirname] =/http Path to localhost//url     [basename] = index.php  //full file name     [extension] + PHP  //filename suffix     [filename] = > index//filename) */?>

Third, the use of parse_url built-in functions

<?php$test = Parse_url ("Http://localhost/index.php?name=tank&sex=1#top");p Rint_r ($test);/* The result is the following array (     [Scheme] + HTTP//Use what protocol     [host] + localhost//hostname     [path] +/index.php//path     [query] = Name=tan K&sex=1//Transmitted Parameters     [fragment] = Top//back root anchor point) */?>

Fourth, the use of basename built-in functions

<?php$test = basename ("Http://localhost/index.php?name=tank&sex=1#top"); echo $test;/* results are as follows Index.php?name= Tank&sex=1#top*/?>

In addition, there is a regular matching process to get the desired value. This method is more accurate, the efficiency is not considered ...
Following the development of the practice of regular processing methods:

<?phppreg_match_all ("/(\w+=\w+) (#\w+)?/I", "http://localhost/index.php?"            Name=tank&sex=1#top ", $match);p Rint_r ($match);/* The result is the following array ([0] = = Array ([0] = Name=tank [1] = = Sex=1#top) [1] = = Array ([0] = Name=tank [1] = = S Ex=1) [2] = = Array ([0] = [1] = + #top)) */?> 

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.