src/Smrtr/HaltoRouter.php
public function match($requestUrl, $requestMethod, $requestHost, $looseMatching = false)
    {
        $params = array();
        $validGroups = $this->getValidHostGroups($requestHost);

        foreach ($this->routes as $handler) {
            list($method, $_route, $target, $name, $hostGroup) = $handler;
            if (null !== $hostGroup) {
                $hostGroups = explode('|', $hostGroup);
                if (!count(array_intersect($hostGroups, $validGroups))) {
                    continue;
                }
            }

            $methods = explode('|', $method);
            $method_match = false;
            foreach ($methods as $method) {
                if (strcasecmp($requestMethod, $method) === 0) {
                    $method_match = true;
                    break;
                }
            }
            if (!$method_match) {
                continue;
            }

            if ($looseMatching) {
                if (substr($_route, -1) === '$') {
                   if (substr($_route, strlen($_route) - 2, 1) === '/') {
                       $_route = sprintf('%s?$', substr($_route, 0, strlen($_route) - 1));
                   } elseif (substr($_route, -2) !== '/?') {
                       $_route = sprintf('%s/?$', substr($_route, 0, strlen($_route) - 1));
                   }
                } elseif (substr($_route, -1) === '/') {
                    $_route = sprintf('%s?', $_route);
                } elseif (substr($_route, -2) !== '/?') {
                    $_route = sprintf('%s/?', $_route);
                }
            } 

            if ($_route === '*') {
                $match = true;
            } elseif (isset($_route[0]) && $_route[0] === '@') {
                $match = preg_match('`' . substr($_route, 1) . '`', $requestUrl, $params);
            } else {
                $route = null;
                $regex = false;
                $j = 0;
                $n = isset($_route[0]) ? $_route[0] : null;
                $i = 0;
                while (true) {
                    if (!isset($_route[$i])) {
                        break;
                    } elseif (false === $regex) {
                        $c = $n;
                        $regex = $c === '[' || $c === '(' || $c === '.';
                        if (false === $regex && false !== isset($_route[$i+1])) {
                            $n = $_route[$i + 1];
                            $regex = $n === '?' || $n === '+' || $n === '*' || $n === '{';
                        }
                        if (false === $regex && $c !== '/' && (!isset($requestUrl[$j]) || $c !== $requestUrl[$j])) {
                            continue 2;
                        }
                        $j++;
                    }
                    $route .= $_route[$i++];
                }
                $regex = $this->compileRoute($route);
                $match = preg_match($regex, $requestUrl, $params);
            }

            if ($match == true || $match > 0) {

                if ($params) {
                    foreach ($params as $key => $value) {
                        if (is_numeric($key)) {
                            unset($params[$key]);
                        } elseif ('controller' == $key) {
                            $controller = preg_replace('#([-]+)#', ' ', urldecode($value));
                            $controller = str_replace(' ', '', ucwords($controller));
                            unset($params[$key]);
                        } elseif ('action' == $key) {
                            $action = preg_replace('#([-]+)#', ' ', urldecode($value));
                            $action = lcfirst(str_replace(' ', '', ucwords($action)));
                            unset($params[$key]);
                        } else {
                            $params[$key] = urldecode($value);
                        }
                    }
                }

                if (isset($controller) || isset($action)) {
                    $targets = explode('@', $target, 2);
                    $C = isset($targets[0]) ? $targets[0] : 'Index';
                    $A = isset($targets[1]) ? $targets[1] : 'index';
                    $target =
                        (isset($controller) ? str_replace('@', '', $controller) : $C)
                        .'@'
                        .(isset($action) ? str_replace('@', '', $action) : $A);
                }

                return array(
                    'target' => $target,
                    'params' => $params,
                    'name' => $name
                );
            }
        }

        return false;
    }
public function match($requestUrl, $requestMethod, $requestHost, $looseMatching = false)
    {
        $params = array();
        $validGroups = $this->getValidHostGroups($requestHost);

        foreach ($this->routes as $handler) {
            list($method, $_route, $target, $name, $hostGroup) = $handler;
            if (null !== $hostGroup) {
                $hostGroups = explode('|', $hostGroup);
                if (!count(array_intersect($hostGroups, $validGroups))) {
                    continue;
                }
            }

            $methods = explode('|', $method);
            $method_match = false;
            foreach ($methods as $method) {
                if (strcasecmp($requestMethod, $method) === 0) {
                    $method_match = true;
                    break;
                }
            }
            if (!$method_match) {
                continue;
            }

            if ($looseMatching) {
                if (substr($_route, -1) === '$') {
                   if (substr($_route, strlen($_route) - 2, 1) === '/') {
                       $_route = sprintf('%s?$', substr($_route, 0, strlen($_route) - 1));
                   } elseif (substr($_route, -2) !== '/?') {
                       $_route = sprintf('%s/?$', substr($_route, 0, strlen($_route) - 1));
                   }
                } elseif (substr($_route, -1) === '/') {
                    $_route = sprintf('%s?', $_route);
                } elseif (substr($_route, -2) !== '/?') {
                    $_route = sprintf('%s/?', $_route);
                }
            } 

            if ($_route === '*') {
                $match = true;
            } elseif (isset($_route[0]) && $_route[0] === '@') {
                $match = preg_match('`' . substr($_route, 1) . '`', $requestUrl, $params);
            } else {
                $route = null;
                $regex = false;
                $j = 0;
                $n = isset($_route[0]) ? $_route[0] : null;
                $i = 0;
                while (true) {
                    if (!isset($_route[$i])) {
                        break;
                    } elseif (false === $regex) {
                        $c = $n;
                        $regex = $c === '[' || $c === '(' || $c === '.';
                        if (false === $regex && false !== isset($_route[$i+1])) {
                            $n = $_route[$i + 1];
                            $regex = $n === '?' || $n === '+' || $n === '*' || $n === '{';
                        }
                        if (false === $regex && $c !== '/' && (!isset($requestUrl[$j]) || $c !== $requestUrl[$j])) {
                            continue 2;
                        }
                        $j++;
                    }
                    $route .= $_route[$i++];
                }
                $regex = $this->compileRoute($route);
                $match = preg_match($regex, $requestUrl, $params);
            }

            if ($match == true || $match > 0) {

                if ($params) {
                    foreach ($params as $key => $value) {
                        if (is_numeric($key)) {
                            unset($params[$key]);
                        } elseif ('controller' == $key) {
                            $controller = preg_replace('#([-]+)#', ' ', urldecode($value));
                            $controller = str_replace(' ', '', ucwords($controller));
                            unset($params[$key]);
                        } elseif ('action' == $key) {
                            $action = preg_replace('#([-]+)#', ' ', urldecode($value));
                            $action = lcfirst(str_replace(' ', '', ucwords($action)));
                            unset($params[$key]);
                        } else {
                            $params[$key] = urldecode($value);
                        }
                    }
                }

                if (isset($controller) || isset($action)) {
                    $targets = explode('@', $target, 2);
                    $C = isset($targets[0]) ? $targets[0] : 'Index';
                    $A = isset($targets[1]) ? $targets[1] : 'index';
                    $target =
                        (isset($controller) ? str_replace('@', '', $controller) : $C)
                        .'@'
                        .(isset($action) ? str_replace('@', '', $action) : $A);
                }

                return array(
                    'target' => $target,
                    'params' => $params,
                    'name' => $name
                );
            }
        }

        return false;
    }
Match a given Request against stored routes.

@param string $requestUrl
@param string $requestMethod
@param string $requestHost
@param bool   $looseMatching

@return array|boolean Array with route information on success, false on failure (no match).