When URL parameters are known, we can use $ _ GET to obtain the relevant parameter information ($ _ GET [\\\ name \\]) According to our own situation. how can we get the parameter information on the URL in an unknown situation? First, use the $ _ SERVER built-in array variable.
When URL parameters are known, we can use $ _ GET to obtain the relevant parameter information ($ _ GET [\ 'name \ ']) based on our own situation. how can we get 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
-
- $ 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
-
- $ 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
- )
- */
- ?>
Type 4: Use the basename built-in function
-
- $ 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 use the regular expression matching method to obtain the required value. this method is more precise and the efficiency is not considered for the moment. below we will expand the regular expression processing method in practice:
-
- 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
- )
- )
- */
- ?>