Three php functions for url parsing

Source: Internet
Author: User
The three php functions used to parse the url are used to pass values through the url, which is an important means of passing values in php. Therefore, we often need to parse the parameters in the url. if we know the url passing parameter name, such as/index. php? Name = tank & amp; sex = 1 # top we can use $ _ GET ['name'], $ _ GET ['sex'] to obtain the three php functions to parse the url

Passing values through URLs is an important means of passing values in php. Therefore, we often need to parse the parameters in the url. if we know the url transfer parameter name, for example

/Index. php? Name = tank & sex = 1 # top

We can use $ _ GET ['name'], $ _ GET ['sex'] to obtain the transmitted data. But what if we don't know the names of these variables? This is also the purpose of writing this blog post, because I always forget to mark it and don't look around again next time.

We can get the url and the parameter string to be passed through the php variable.

$ _ SERVER ["QUERY_STRING"] name = tank & sex = 1

$ _ SERVER ["REQUEST_URI"]/index. php? Name = tank & sex = 1

Javascript can also obtain the source url, document. referrer; there are many methods

1. use pathinfo


1. 2. $ test = pathinfo ("http: // localhost/index. php ");??
3. print_r ($ test );??
4.?> ??
5. The result is as follows ??
6. Array ??
7 .(??
8 .??? [Dirname] => http: // localhost // url path ??
9 .??? [Basename] => index. php? // Complete file name ??
10 .??? [Extension] => php? // File name suffix ??
11 .??? [Filename] => index // file name ??
12 .)?
$ Test = pathinfo ("http: // localhost/index. php ");
Print_r ($ test );
?>
The result is as follows:
Array
(
??? [Dirname] => http: // localhost // url path
??? [Basename] => index. php? // Complete file name
??? [Extension] => php? // File name suffix
??? [Filename] => index // file name
) 2. use parse_url


1. 2. $ test = parse_url ("http: // localhost/index. php? Name = tank & sex = 1 # top ");??
3. print_r ($ test );??
4.?> ??
5. The result is as follows ??
6. Array ??
7 .(??
8 .??? [Scheme] => http // what protocol is used ??
9 .??? [Host] => localhost // host name ??
10 .??? [Path] =>/index. php // path ??
11 .??? [Query] => name = tank & sex = 1 // The passed parameter ??
12 .??? [Fragment] => top // The anchor behind the root ??
13 .)?
$ Test = parse_url ("http: // localhost/index. php? Name = tank & sex = 1 # top ");
Print_r ($ test );
?>
The result is as follows:
Array
(
??? [Scheme] => http // what protocol is used
??? [Host] => localhost // host name
??? [Path] =>/index. php // path
??? [Query] => name = tank & sex = 1 // The passed parameter
??? [Fragment] => top // The anchor behind the root
) 3. use basename


1. 2. $ test = basename ("http: // localhost/index. php? Name = tank & sex = 1 # top ");??
3. echo $ test ;??
4.?> ??
5. The result is as follows ??
6. index. php? Name = tank & sex = 1 # top?
$ Test = basename ("http: // localhost/index. php? Name = tank & sex = 1 # top ");
Echo $ test;
?>
The result is as follows:
Index. php? Name = tank & sex = 1 # top three methods above, we can basically get what we want. In fact, another method is to use regular expressions and quickly obtain the data we think.

There are many parameter passing methods, but there are mainly two of them. one is, name = tank & sex = 1 # top; the other is, name = tank & sex = 1.


1. 2. preg_match_all ("/(\ w + = \ w +) (# \ w + )? /I "," http: // localhost/index. php? Name = tank & sex = 1 # top ", $ match );??
3. print_r ($ match) ;?> ??
4. The result is as follows ??
5. Array ??
6 .(??
7 .??? [0] => Array ??
8 .??????? (??
9 .??????????? [0] => name = tank ??
10 .??????????? [1] => sex = 1 # top ??
11 .??????? )??
12 .??? [1] => Array ??
13 .??????? (??
14 .??????????? [0] => name = tank ??
15 .??????????? [1] => sex = 1 ??
16 .??????? )??
17 .??? [2] => Array ??
18 .??????? (??
19 .??????????? [0] => ??
20 .??????????? [1] = ># top ??
21 .??????? )??
22 .)?
Preg_match_all ("/(\ w + = \ w +) (# \ w + )? /I "," http: // localhost/index. php? Name = tank & sex = 1 # top ", $ match );
Print_r ($ match);?>
The result is 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
??????? )
) All the required data is matched. it takes a long time to get regular expressions, and it is a bit new. The rules in the above regular expression are not dead. The rules are inferred based on URLs.

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.