http:
$route
=
array
(
'news/:action^
delete
|update|insert/:year\d/:month/:day'=>
array
('news/read?extra=:2&status=1','year=:2&month=:3&day=:4'),
'news/:action/:year\d/:month/:day'=>'news/read?year=:2&month=:3&day=:4'
);
$url
= 'http:
$extension
= 'html';
$regx
= 'news/read/2012/2/21/extraparam/test.html';
foreach
(
$route
as
$key
=>
$value
){
if
(parseUrlRule(
$key
,
$value
,
$regx
,
$extension
))
break
;
}
function
parseUrlRule(
$rule
,
$route
,
$regx
,
$extension
=null){
!
is_null
(
$extension
) &&
$regx
=
str_replace
('.'.
$extension
,'',
$regx
);
$ruleArr
=
explode
('/',
$rule
);
$regxArr
=
explode
('/',
$regx
);
$url
=
is_array
(
$route
) ?
$route
[0] :
$route
;
$match
=true;
foreach
(
$ruleArr
as
$key
=>
$value
){
if
(
strpos
(
$value
,':')===0){
if
(
substr
(
$value
,-2)=='\\d' && !
is_numeric
(
$regxArr
[
$key
])){
$match
= false;
break
;
}
elseif
(
strpos
(
$value
,'^')){
$stripArr
=
explode
('|',trim(
strstr
(
$value
,'^'),'^'));
if
(in_array(
$regxArr
[
$key
],
$stripArr
)){
$match
= false;
break
;
}
}
}
elseif
(
strcasecmp
(
$value
,
$regxArr
[
$key
])!==0) {
$match
= false;
break
;
}
}
if
(
$match
){
foreach
(
$ruleArr
as
$key
=>
$value
){
if
(
strpos
(
$value
,':')===0){
if
(
substr
(
$value
,-2,1)=='\\')
$matchKey
=
substr
(
$value
,1,-2);
elseif
(
$pos
=
strpos
(
$value
,'^'))
$matchKey
=
substr
(
$value
,1,
$pos
-1);
else
$matchKey
=
substr
(
$value
,1);
$matches
[
$matchKey
] =
array_shift
(
$regxArr
);
}
else
array_shift
(
$regxArr
);
}
$values
=
array_values
(
$matches
);
$url
= preg_replace('/:(\d+)/e','
$values
[\\1-1]',
$url
);
if
(
strpos
(
$url
,'?')!==false){
$arr
=
parse_url
(
$url
);
$paths
=
explode
('/',
$arr
['path']);
parse_str
(
$arr
['query'],
$queryArr
);
}
elseif
(
strpos
(
$url
,'/')!==false)
$paths
=
explode
('/',
$url
);
else
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
=
array_merge
(
$_GET
,
$var
);
if
(isset(
$queryArr
))
$_GET
=
array_merge
(
$_GET
,
$queryArr
);
preg_replace('/(\w+)\/([^,\/]+)/e','
$tempArr
[\'\\1\']=\'\\2\'',implode('/',
$regxArr
));
if
(!
empty
(
$tempArr
))
$_GET
=
array_merge
(
$_GET
,
$tempArr
);
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
='?';
if
(!
empty
(
$tempArr
))
$var
=
array_merge
(
$tempArr
,
$var
);
$url
.=
$der
.http_build_query(
$var
);
}
$_GET
['finalUrl'] =
$url
;
$_REQUEST
=
array_merge
(
$_REQUEST
,
$_GET
);
print_r(
$_GET
);
return
true;
}
return
$match
;
}