This article describes how to parse a url in php, obtain url parameters, and obtain url parameters in four ways. It involves converting string parameters into arrays and converting parameters into strings, the code in this article is easy to understand. If you are interested, let's take a look at the following code: php parses the url and obtains the parameters in the url. The Code is as follows:
<? Php $ url =' http://www.baidu.com/index.php?m=content&c=index&a=lists&catid=6&area=0&author=0&h=0®ion=0&s=1&page=1 '; $ Arr = parse_url ($ url); var_dump ($ arr); $ arr_query = convertUrlQuery ($ arr ['query']); var_dump ($ arr_query ); var_dump (getUrlQuery ($ arr_query);/*** convert the string parameter to an array * @ param $ query * @ return array (size = 10) 'M' => string 'content' (length = 7) 'c' => string 'index' (length = 5) 'A' => string 'lists' (length = 5) 'catid' => string '6' (length = 1) 'area '=> string '0' (length = 1) 'author' => string '0' (length = 1) 'H' => string '0' (length = 1) 'region' => string '0' (length = 1) 'S '=> string '1' (length = 1) 'page' => string '1' (length = 1) */function convertUrlQuery ($ query) {$ queryParts = explode ('&', $ query); $ params = array (); foreach ($ queryParts as $ param) {$ item = explode ('= ', $ param); $ params [$ item [0] = $ item [1];} return $ params ;} /*** change the parameter to a string * @ param $ array_query * @ return string'm = content & c = index & a = lists & catid = 6 & area = 0 & author = 0 & h = 0 & region = 0 & s = 1 & page = 1' (length = 73) */function getUrlQuery ($ array_query) {$ tmp = array (); foreach ($ array_query as $ k => $ param) {$ tmp [] = $ k. '= '. $ param;} $ params = implode ('&', $ tmp); return $ params ;}
The following describes how to obtain php url parameters through four examples.
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 // protocol used [host] => localhost // host name [path] =>/index. php // path [query] => name = tank & sex = 1 // The 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:
<?phppreg_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 ))*/?>