Four examples of how PHP URL parameters are obtained

Source: Internet
Author: User

  This article mainly introduces the PHP URL parameter to obtain the way four examples, php URL parameter parsing 4 kinds of methods, the need friend may refer to under

In the case of a known URL parameter, we can use $_get to obtain the corresponding parameter information ($_get[' name ') according to our own situation; then how do I get the parameter information on the URL in the unknown case?   The first, using the $_server built-in array variable   The relatively original $_server[' query_string '] to get the parameters of the URL, usually returned by using this variable will be similar to the data:name=tank& Sex=1 You can use $_server["Request_uri" If you need to include a filename (return similar:/index.php?name=tank&sex=1)   Second, take advantage of pathinfo built-in functions     Code as follows: <?php $test = PathInfo ("http://localhost/index.php"); Print_r ($test); /* results are as follows Array (     [dirname] => http://localhost//url path      [basename] => INDEX.P HP  //full file name      [extension] => php  //filename suffix      [filename] => index//file name )/?>     The third, the use of Parse_url built-in functions     code as follows: <?php $test = Parse_url ("Http://localhost/index.php?na Me=tank&sex=1#top "); Print_r ($test); /* results are as follows Array (     [scheme] => http//Use what protocol      [host] => localhost//host name   & nbsp  [path] =>/index.php//path      [query] => namE=tank&sex=1//Transmitted parameters      [fragment] => Top//back root anchor) * *?>     Fourth, using basename built-in functions &N Bsp     Code as follows: <?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, it is through the regular matching process to get the desired value. This method is more accurate, the efficiency is not considered ... Below expand practice under the regular processing mode:     code as follows: <?php Preg_match_all ("/(w+=w+) (#w +)?/I", "Http://localhost/index.php?name=tank &sex=1#top ", $match); Print_r ($match); /* results are as follows Array (    [0] => Array         (            [0] => Name=tank             [1] => sex=1#top        )     [1] => Array           (            [0] => Name=tank   & nbsp          [1] => sex=1          )      [2] => ArraY         (             [0] =>         &NBS P   [1] => #top        )] * *?>       long journey ... Still need to dig ...  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.