PHP to parse the URL and get the parameters in the URL and get the URL parameter four ways _php instance

Source: Internet
Author: User
Tags explode

The following code is the PHP resolution URL and gets the parameters in the URL, as shown in the following code:

<?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));
          /** * Changes the string parameter to an array * @param $query * @return array Array (size=10) ' m ' => string ' content ' (length=7) ' C ' => string ' index ' (length=5) ' a ' => string ' lists ' (length=5) ' catid ' => string ' 6 ' ( length=1) ' area ' => string ' 0 ' (length=1) ' author ' => string ' 0 ' (length=1) ' H ' => String ' 0 ' (length=1) ' region ' => string ' 0 ' (length=1) ' s ' => string ' 1 ' (length=1) '
  Page ' => string ' 1 ' (length=1)/function Converturlquery ($query) {$queryParts = Explode (' & ', $query);
  $params = Array ();
   foreach ($queryParts as $param) {$item = explode (' = ', $param); $params [$item [0]] = $item [1];
return $params; /** * Changes the argument to a string * @param $array _query * @return string string ' m=content&c=index&a=lists&catid=6&area= 0&author=0&h=0&region=0&s=1&page=1 ' (length=73)/function Geturlquery ($array _query) {$tmp = arr
  Ay ();
  foreach ($array _query as $k => $param) {$tmp [] = $k. ' = '. $param;
  } $params = Implode (' & ', $tmp);
return $params; }

Below through four kinds of examples to introduce PHP URL parameter to get the way.

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, the use of $_server built-in array variables

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

The second, the use of pathinfo built-in functions

The code is as follows:

<?php
$test = PathInfo ("http://localhost/index.php");
Print_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//filename
)
*/
?>

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");
Print_r ($test);
/*

The results are as follows

Array
(
   [scheme] => HTTP///using what Protocol
   [host] => localhost//host name
   [path] =>/index.php//Path
   [ Query] => name=tank&sex=1//passed parameters
   [fragment] => the anchor point of Top//back root
)
* * *
?>

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 the way to get the value you need through a regular matching process. This method is more accurate, the efficiency is not considered ...
Below expand practice under the regular treatment method:

The code is as follows:

<?php
Preg_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
    )
)
*/
?>

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.