Examples of PHP four URL resolution processing methods

Source: Internet
Author: User

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
<?php
$test = PathInfo ("http://localhost/index.php");
Print_r ($test);
04/*
05 results are as follows
-Array
07 (
=> http://localhost//url path for [dirname]
[basename] => index.php//Full file name
[extension] => PHP//filename suffix
[FileName] => index//filename
12)
13 */
?>


Third, the use of Parse_url built-in functions
<?php
$test = Parse_url ("Http://localhost/index.php?name=tank&sex=1#top");
Print_r ($test);
04/*
05 results are as follows
-Array
07 (
[Scheme] => HTTP//What protocol is used
[Host] => localhost//host name
Ten [path] =>/index.php//path
[query] => name=tank&sex=1//passed parameters
[Fragment] => Top//back root anchor Point
13)
14 */
?>


Fourth, the use of basename built-in functions
1 <?php
2 $test = basename ("Http://localhost/index.php?name=tank&sex=1#top");
3 echo $test;
4/*
5 results are as follows
6 Index.php?name=tank&sex=1#top
7 */
8?>


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:
<?php
Preg_match_all ("/(\w+=\w+) (#\w+)?/I", "Http://localhost/index.php?name=tank&sex=1#top", $match);
Print_r ($match);
04/*
05 results are as follows
-Array
07 (
[0] => Array
09 (
Ten [0] => Name=tank
One [1] => Sex=1#top
12)
[1] => Array
14 (
[0] => Name=tank
[1] => sex=1
17)
KM [2] => Array
19 (
[0] =>
[1] => #top
22)
23)
24 */
?>


The journey is long ... Still need to dig ...

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.