0

所以我用PHP做了一个路由系统......

所以我创建了一个名为 /post/:id 的路由,但是每当我 print_r my $matches 时,我都会得到:

数组 ( [0] => /post/10 [d] => 10 [1] => 10 ) 1

数组中的'd'显然应该是'id',有人知道如何解决这个问题吗?

谢谢

    <?php
    public function setPattern($pattern)
    {
            $this->_pattern = $pattern;
            $this->_regex = preg_replace('#:([a-z])+$#', "(?P<$1>[^/]+)", $pattern);
    }

    public function match($uri)
    {

            if (!preg_match("#" . $this->_regex . "$#", $uri, $matches))
            {
                    return false;
            }
            else
            {
                      return $matches;
            }
    }
4

1 回答 1

0

您只能在preg_replace通话中捕获一个角色。试试这个并将+号放在捕获中以获取多个字符:

$this->_regex = preg_replace('#:([a-z]+)$#', "(?P<$1>[^/]+)", $pattern);
于 2012-12-06T07:59:03.193 回答