Four Examples of How to Obtain PHP URL Parameters_PHP Tutorial

WBOY
Release: 2016-07-13 10:36:56
Original
926 people have browsed it

When the URL parameters are known, we can use $_GET to obtain the corresponding parameter information ($_GET['name']) according to our own situation; then, how to obtain the parameter information on the URL when the URL is unknown? Woolen cloth?

The first method is to use $_SERVER built-in array variable

The relatively primitive $_SERVER['QUERY_STRING'] is used to obtain URL parameters. Usually, this variable is used to return data similar to this: name=tank&sex=1
If you need to include the file name, you can use it. $_SERVER["REQUEST_URI"](return similar:/index.php?name=tank&sex=1)

The second method is to use the pathinfo built-in function


Copy code The code is as follows:

$test = pathinfo("http://localhost/index.php");
print_r($test);
/*
The result is as follows
Array
(
[dirname] = > http://localhost //The path of the URL
[basename] => index.php //Full file name
[extension] => php //File name suffix
[filename] => index //File name
)
*/
?>

The third method is to use the parse_url built-in function


Copy code The code is as follows:

$test = parse_url("http: //localhost/index.php?name=tank&sex=1#top");
print_r($test);
/*
The results are as follows
Array
(
[scheme ] => http //What protocol to use? [host] => localhost //Host name
[path] => /index.php //Path
[query] => name =tank&sex=1 // The parameters passed
[fragment] => top //The anchor point of the back root
)
*/
?>

The fourth method is to use the basename built-in function


Copy code The code is as follows:
$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, you can obtain the required value through regular matching. This method is more accurate and efficiency is not considered for the time being. . .

The following expands the regular processing method in practice:

Copy the code The code is as follows:
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
)
)
*/
?>

The road is long...still needs to be explored...

http://www.bkjia.com/PHPjc/736837.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/736837.htmlTechArticleWhen the URL parameters are known, we can use $_GET to obtain the corresponding parameter information according to our own situation ( $_GET['name']); Then, how to get the parameter information on the URL under unknown circumstances...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template