PHP parse url and get URL parameters and get URL parameters of four ways, url four kinds of _php tutorial

Source: Internet
Author: User

PHP parse the URL and get the URL parameters and get URL parameters of four ways, url four kinds of


The following code is the PHP parse URL and get the parameters in the URL, the code is as follows:

<?php$url = ' http://www.baidu.com/index.php?m=content&c=index&a=lists&catid=6&area=0& Author=0&h=0&region=0&s=1&page=1 '; $arr = Parse_url ($url); Var_dump ($arr); $arr _query = Converturlquery ($arr [' query ']); Var_dump ($arr _query); Var_dump (Geturlquery ($arr _query));/** * Change string parameter to array * @param $ Query * @return array Array (size=10) ' m ' = + string ' content ' (length=7) ' c ' + = String ' index ' (le ngth=5) ' a ' = = String ' lists ' (length=5) ' catid ' = = String ' 6 ' (length=1) ' area ' = = St Ring ' 0 ' (length=1) ' Author ' = = String ' 0 ' (length=1) ' h ' = = String ' 0 ' (length=1) ' Regio n ' = = String ' 0 ' (length=1) ' s ' = = String ' 1 ' (length=1) ' page ' = = String ' 1 ' (length=1) */func  tion Converturlquery ($query) {$queryParts = Explode (' & ', $query);  $params = Array ();    foreach ($queryParts as $param) {$item = explode (' = ', $param); $params [$item [0]] = $item [1]; } return $params;} /** * Change parameter to String * @param $array _query * @return String ' m=content&c=index&a=lists&catid=6&area=0& Amp;author=0&h=0&region=0&s=1&page=1 ' (length=73) */function geturlquery ($array _query) {$tmp = array  ();  foreach ($array _query as $k + $param) {$tmp [] = $k. ' = '. $param;  } $params = Implode (' & ', $tmp); return $params;}

The following four kinds of examples to introduce the PHP URL parameter acquisition method.

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

The code is as follows:

<?php$test = PathInfo ("http://localhost/index.php");p Rint_r ($test);/*

The results are as follows

Array (   [dirname] = http://localhost//url path   [basename] = index.php//full file name   [extension] + PHP/ /filename suffix   [filename] = = index//file name) */?>

Third, the use of parse_url built-in functions

The code is as follows:

<?php$test = Parse_url ("Http://localhost/index.php?name=tank&sex=1#top");p Rint_r ($test);/*

The results are as follows

Array (   [scheme] + HTTP//Use what protocol   [host] + localhost//hostname   [path] =/index.php//path   [query] =& Gt Name=tank&sex=1//Transmitted Parameters   [fragment] = Top//back root anchor point) */?>

Fourth, the use of basename built-in functions

The code is as follows:

<?php$test = basename ("Http://localhost/index.php?name=tank&sex=1#top"); echo $test;/*

The 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:

The code is as follows:

<?phppreg_match_all ("/(\w+=\w+) (#\w+)?/I", "Http://localhost/index.php?name=tank&sex=1#top", $match); Print_r ($match);/*

The results are as follows

Array ([  0] = = Array    (      [0] = = Name=tank      [1] = Sex=1#top    )  [1] = = Array     (      [ 0] = = Name=tank       [1] = Sex=1     )   [2] = = Array    (       [0] =      [1] = #top    )) */? >

http://www.bkjia.com/PHPjc/1065159.html www.bkjia.com true http://www.bkjia.com/PHPjc/1065159.html techarticle php parse the URL and get the URL parameters and get the URL parameters of four ways, URL four the following paragraph of code is the PHP parse URL and get the URL parameters, the code is as follows: Php$url = ' http: ...

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