PHP:调用preg_match的搜索顺序的解决方案

I took the suggested solution from PHP: Good solution for MVC routing function to parse requested views? and adujsted it to our internal guidelines (Thanks again Supericy).

I have now following code:

public function parseRequestedView($url) {

    $this->view_requested = explode('/', trim($url, '/'));

    // Format: [0] viewpoint, [1] child, [2] action (default: show), [3] reference
    $this->view_map = array(
        $this->parseViewMapArrayKey('device/:reference:/configurations') => array(0,  2, -1,  1),
        $this->parseViewMapArrayKey('device/:reference:')                => array(0, -1, -1,  1)
    );

    foreach ($this->view_map as $this->view_map_transitory => $this->view_map_indices) {

        if (preg_match($this->view_map_transitory, $url)) {

            foreach ($this->view_map_indices as $this->view_index) {

                $this->view_resources[] = $this->view_index > -1 ? $this->view_requested[$this->view_index] : null;

            }

            return $this->view_resources;

        }

    }

    return false;

}

public function parseViewMapArrayKey($key) {

    return '#'.str_replace([":reference:", ":string:"], ["\d+", ".+"], $key).'#';

}

It is working fine, except one small "problem":

When I switch the keys "device/:reference:" and "device/:reference:/configurations" and then invoke the view for device/:reference:/configurations, I only get the result for device/:reference.

For example, http://ww.foo.com/device/123456/configurations will output:

Array
(
    [0] => device
    [1] => 
    [2] => 
    [3] => 123456
)

Which is the result for http://ww.foo.com/device/123456. When I change the keys back to their original order, everything is like it should be:

Array
(
    [0] => device
    [1] => configurations
    [2] => 
    [3] => 123456
)

How can I reverse the search order or make the function output the right result when I switch the keys?

I found this PHP Reverse Preg_match. But it is about a negative preg_match as far as I can tell. Or I am wrong?

Thanks in advance.

OK maybe this is a dumb answer but perhaps you could make sure that your regex starts with ^ and ends with $?

For instance

public function parseViewMapArrayKey($key) {

    return '#^'.str_replace([":reference:", ":string:"], ["\d+", ".+"], $key).'$#';

}