Home > Article > Backend Development > PHP parses the url and gets the parameters in the url and four ways to get the url parameters, four types of url_PHP tutorial
The following piece of code is php parsing the url and getting the parameters in the url. The code looks like this:
<?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)); /** * 将字符串参数变为数组 * @param $query * @return array 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; } /** * 将参数变为字符串 * @param $array_query * @return string string 'm=content&c=index&a=lists&catid=6&area=0&author=0&h=0®ion=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 uses four examples to introduce how to obtain php url parameters.
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 $_SERVER["REQUEST_URI"] (return similar: /index.php?name=tank&sex=1)
The second method is to use the pathinfo built-in function
The code is as follows:
<?php $test = pathinfo("http://localhost/index.php"); print_r($test); /*
The results are as follows
Array ( [dirname] => http://localhost //url的路径 [basename] => index.php //完整文件名 [extension] => php //文件名后缀 [filename] => index //文件名 ) */ ?>
The third method is to 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 results are as follows
Array ( [scheme] => http //使用什么协议 [host] => localhost //主机名 [path] => /index.php //路径 [query] => name=tank&sex=1 // 所传的参数 [fragment] => top //后面根的锚点 ) */ ?>
The fourth method is to use the basename built-in function
The code is as follows:
<?php $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:
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 results are 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 ) ) */ ?>