//解析url 格式: 分组/模块/操作?key1=value1&key2=value2
if(strpos($url,'?')!==false){
// 分组/模块/操作?key1=value1&key2=value2
$arr = parse_url($url);
$paths = explode('https://www.jb51.net/',$arr['path']);
parse_str($arr['query'],$queryArr);
}elseif(strpos($url,'https://www.jb51.net/')!==false) //分组/模块/操作)
$paths = explode('https://www.jb51.net/',$url);
else // key1=value1&key2=value2
parse_str($url,$queryArr);
//获取 分组 模块 操作
if(!empty($paths)){
$var['actionName'] = array_pop($paths);
$var['moduleName'] = array_pop($paths);
if(!empty($paths)){
$groupList = 'Home,Admin';
$temp = array_pop($paths);
if(in_array($temp,explode(',',$groupList)))
$var['groupName'] = $temp;
}
}
//合并的到GET数组中,方便全局调用
$_GET = array_merge($_GET,$var);
//合并参数
if(isset($queryArr))
$_GET = array_merge($_GET,$queryArr);
//匹配url中剩余的参数
preg_replace('/(\w+)\/([^,\/]+)/e','$tempArr[\'\\1\']=\'\\2\'',implode('https://www.jb51.net/',$regxArr));
if(!empty($tempArr))
$_GET = array_merge($_GET,$tempArr);
//route是数组的话
if(is_array($route)){
$route[1]=preg_replace('/:(\d+)/e','$values[\\1-1]',$route[1]);
parse_str($route[1],$var);
$_GET = array_merge($_GET,$var);
strpos($url,'?')!==false ? $der ='&' : $der='?';
//最终写入到$_GET中的参数,包括三个部分
//1.地址栏剩余参数
//2.路由地址中的参数
//3.$route是数组时的第二个参数
if(!empty($tempArr))
$var = array_merge($tempArr,$var);
$url .=$der.http_build_query($var);
}
$_GET['finalUrl'] = $url;
//保证$_REQUEST 也能访问
$_REQUEST = array_merge($_REQUEST,$_GET);
//结果
print_r($_GET);
return true;
}
return $match;
}
//以下是正则路由代码:
$rule = '/news\/read\/(\d+)\/(\d+)\/(\d+)/';
$route ='news/read?year=:1&month=:2&day=:3';
$regx = 'news/read/2012/2/21/extraparam/test.html';
$extension = 'html';
parseUrlRuleRegx($rule,$route,$regx,$extension);
/**
* @$rule string 路由规则
* @$route string 规则映射的新地址
* @$regx string 地址栏pathinfo字符串
* @$extension stirng 伪静态拓展名
* return bool
*/
function parseUrlRuleRegx($rule,$route,$regx,$extension=null){
!is_null($extension) && $regx = str_replace('.'.$extension,'',$regx);
$url = is_array($route) ? $route[0] : $route;
if(preg_match($rule,$regx,$matches)){
$url = preg_replace('/:(\d+)/e','$matches[\\1]',$url);
}else
return false;
//解析url 格式: 分组/模块/操作?key1=value1&key2=value2
if(strpos($url,'?')!==false){
// 分组/模块/操作?key1=value1&key2=value2
$arr = parse_url($url);
$paths = explode('https://www.jb51.net/',$arr['path']);
parse_str($arr['query'],$queryArr);
}elseif(strpos($url,'https://www.jb51.net/')!==false) //分组/模块/操作)
$paths = explode('https://www.jb51.net/',$url);
else // key1=value1&key2=value2
parse_str($url,$queryArr);