有没有更好的方法或优雅的方法来做到这一点?我一直在使用此代码根据提供的模式检查数据库中的类似 url。
$menu_selected = '';
$uri_array = explode('/','commodity/statement/flour/factory/');
$menus = array(
'commodity/search/flour/factory/index',
'commodity/statement/sugar/branch/index'
);
$pattern_parts = explode('/', '=/*/=/=/*');
foreach ($menus as $menu) {
$url_parts = explode('/',$menu);
if( count($pattern_parts) == count($url_parts) ){
#[i] Trim down for wildcard pattern
foreach($pattern_parts as $i => $part ){
if( $part == '*' ) {
unset($pattern_parts[$i]);
unset($url_parts[$i]);
unset($uri_array[$i]);
}
}
#[i] Join array and compare
if( join('/', $uri_array) == join('/', $url_parts) ) {
// Found and return the selected
$menu_selected = $menu;
break;
}
}
}