Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中检查URL的更好方法_Php_Arrays_Pattern Matching - Fatal编程技术网

在PHP中检查URL的更好方法

在PHP中检查URL的更好方法,php,arrays,pattern-matching,Php,Arrays,Pattern Matching,有没有更好的方法或优雅的方式来做到这一点?我一直在使用这段代码根据提供的模式检查数据库中类似的url $menu_selected = ''; $uri_array = explode('/','commodity/statement/flour/factory/'); $menus = array( 'commodity/search/flour/factory/index', 'commodity/statement/sugar/branch/

有没有更好的方法或优雅的方式来做到这一点?我一直在使用这段代码根据提供的模式检查数据库中类似的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;
        }
    }
}

不是答案,但请停止使用别名,只使用
内爆()
。这是一个答案:为什么不使用正则表达式模式来匹配数据库中的记录?在这种情况下,听起来是一个开始学习的绝佳机会,对错:如果您想要更好的方法,请学习正则表达式。哈哈,当然。有好的教程吗?