PHP preg_match正则表达式错误

时间:2017-10-03 12:07:53

标签: php regex

如果我在php中运行preg_match

,我就会成为正则表达式错误

这是我想要分析的字符串

/backoffice/1

这是我的正则表达式

\/backoffice\/(.*)\/?(.*)?

我在这个网站https://regex101.com/检查了这个正则表达式,这个网站说它一切正常,正则表达式在这个字符串中找到但是如果我在php上运行这个我就成了这个错误

  

preg_match():分隔符不能是字母数字或反斜杠

在这种情况下我必须改变什么才能使它发挥作用?

php代码就是这个

$routes = [

    'backoffice' => ['controller' => 'pages','action' => 'index','params' => ['arg1','arg2' => null]],
    'backoffice/testo' => ['controller' => 'pages','action' => 'index2','params' => ['arg1']],
    'backoffice/test/test1/test2' => ['controller' => 'pages','action' => 'index3','params' => ['arg1','arg2']]

];

$url = '/'.$url;

        foreach($routes as $r => $route){

            $searchPattern = $r;
            $searchPattern = str_replace("/", "\/", $searchPattern);

            if(isset($route['params'])){

                foreach($route['params'] as $p => $param){

                    if($param == null){
                        $searchPattern = $searchPattern.'\/?(.*)?';
                    } else {
                        $searchPattern = $searchPattern.'\/(.*)';
                    }

                }

            }

            $searchPattern = '\/'.$searchPattern;
            var_dump($searchPattern);
            preg_match($searchPattern, $url, $matches);

        }
        var_dump($matches);
        exit;

0 个答案:

没有答案