Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.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_Regex_Validation_Url Rewriting_Url Routing - Fatal编程技术网

Php 替换用于url比较的通配符

Php 替换用于url比较的通配符,php,regex,validation,url-rewriting,url-routing,Php,Regex,Validation,Url Rewriting,Url Routing,我需要检查一个路由文件中的有效路由,我想在其中为动态url部分放置一个通配符(或占位符)。 路由器读取该json格式的所有路由: {"action" : "BlogController@showPost", "method" : "GET", "url" : "showPost/id/{}"} 我需要在比较发生时用当前值更改holder{any},并允许将regex表达式放入{any}标记中 像这样的url: showPost/id/211必须与showPost/id/{}进行比较,并应返回t

我需要检查一个路由文件中的有效路由,我想在其中为动态url部分放置一个通配符(或占位符)。 路由器读取该json格式的所有路由:

{"action" : "BlogController@showPost", "method" : "GET", "url" : "showPost/id/{}"}
我需要在比较发生时用当前值更改holder{any},并允许将regex表达式放入{any}标记中

像这样的url: showPost/id/211必须与showPost/id/{}进行比较,并应返回true。如果可能,我希望允许将{'[0-9]\'}作为可选参数,以确保实值与正则表达式匹配

什么是最好的解决方案

比较方法如下:

    public static function findAction($query) {
       foreach (Router::getInstance()->routes as $route) {
         if ($route->url == $query) {
             return $route;
         }
     }
}
$query包含/showPost/id/221,Router::getInstance()->routes->route->url包含showPost/id/{}

该帖子与自动解决的问题相关: 为了避免重复,我不会重新发布路由器代码

提前感谢

我找到了一个解决方案,使用“?”作为json文件的通配符。这也许不是最好的方法,但实际上是有效的。 该方法现在用替换(并尝试检查)实际路径查询?并检查每个周期的路线

public static function findAction($query) {
        //d($query);
        $queryArray = explode("/", $query);
        //d($queryArray);
           foreach (Router::getInstance()->routes as $route) {
             if ($route->url == $query) {
                 // replace current routes url with incoming url
                 $route->url = $query;
                 return $route;
             } else {
                 $queryReplace = null;
                 foreach ($queryArray as $key => $value) {
                     if (strpos($route->url,"?")) {
                         $queryReplace = str_replace("?", $value, $route->url);
                         if($queryReplace == $query) {
                             $route->url = $query;
                             return $route;
                         }
                     }
                 }

             }
我仍然想把{any或regex}放在atm上,但我没有找到解决这个问题的方法