php解析url并得到url中的参数
1 <?php 2 $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'; 3 $arr = parse_url($url); 4 var_dump($arr); 5 $arr_query = convertUrlQuery($arr['query']); 6 var_dump($arr_query); 7 var_dump(getUrlQuery($arr_query)); 8 /** 9 * 将字符串参数变为数组 10 * @param $query 11 * @return array array (size=10) 12 'm' => string 'content' (length=7) 13 'c' => string 'index' (length=5) 14 'a' => string 'lists' (length=5) 15 'catid' => string '6' (length=1) 16 'area' => string '0' (length=1) 17 'author' => string '0' (length=1) 18 'h' => string '0' (length=1) 19 'region' => string '0' (length=1) 20 's' => string '1' (length=1) 21 'page' => string '1' (length=1) 22 */ 23 function convertUrlQuery($query) 24 { 25 $queryParts = explode('&', $query); 26 $params = array(); 27 foreach ($queryParts as $param) { 28 $item = explode('=', $param); 29 $params[$item[0]] = $item[1]; 30 } 31 return $params; 32 } 33 /** 34 * 将参数变为字符串 35 * @param $array_query 36 * @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) 37 */ 38 function getUrlQuery($array_query) 39 { 40 $tmp = array(); 41 foreach($array_query as $k=>$param) 42 { 43 $tmp[] = $k.'='.$param; 44 } 45 $params = implode('&',$tmp); 46 return $params; 47 }