Four methods for obtaining php url parameters

Source: Internet
Author: User

Four methods for obtaining php url parameters

This article mainly introduces four examples of php url parameter acquisition methods and four methods of php url Parameter Parsing. For more information, see

When URL parameters are known, we can use $ _ GET to obtain the relevant parameter information ($ _ GET ['name']) according to our own situation, how can I obtain the parameter information on the URL in an unknown situation?

 

First, use the $ _ SERVER built-in array variable

 

Obtain the URL parameter from the original $ _ SERVER ['query _ string']. Generally, this variable returns data similar to this: name = tank & sex = 1.

To include a file name, you can use $ _ SERVER ["REQUEST_URI"] (the returned result is similar to:/index. php? Name = tank & sex = 1)

 

Second, use pathinfo built-in functions

 

The Code is as follows:

<? Php

$ 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

)

*/

?>

 

 

Third, use the parse_url built-in Function

 

The Code is as follows:

<? Php

$ 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

)

*/

?>

 

 

Fourth, use the built-in functions of basename

 

 

The Code is as follows:

<? Php

$ 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

*/

?>

 

 

In addition, you can obtain the required value through regular expression matching. This method is more accurate, and the efficiency is not considered for the moment...

Below we will expand the Regular Expression Processing Method in practice:

 

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

)

)

*/

?>

 

 

 

Long journey... waiting to continue mining...

 

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.