phapi/middleware-route · RouteParser.php
php logo
public function parse($route)
    {
        // reset param names
        $this->paramNames = [];

        // replace shortcuts
        $regex = strtr($route, $this->shortcuts);

        // replace param names that doesn't include shortcuts or regex
        $regex = preg_replace_callback('/{([a-zA-Z0-9]+)}/', [$this, 'replaceWithoutRegex'], $regex);

        // replace param names that includes shortcuts or regex
        $regex = preg_replace_callback('/{([a-zA-Z0-9]+):/', [$this, 'replaceWithRegex'], $regex);

        // replace ending grouping
        $regex = str_replace('}', ')', $regex);

        // fix slashes when optional params are present
        $regex = $this->fixOptionalSlashes($regex);

        // fix for handling trailing slashes
        $regex .= '(/)?';

        // return regex and param names
        return ['#^'. $regex .'$#', $this->paramNames];
    }
Similar code snippets
1.
samurai-fw/samurai · GlobExpression.php
Match rating: 69.7% · See similar code snippets
php logo
public function getRegexPattern()
    {
        $regex = preg_quote($this->value, '/');
        $regex = substr($regex, -1) !== '*' ? $regex . '$' : $regex;
        $regex = preg_replace('/\\\\\\*/', '.*?', $regex);
        return "/^{$regex}/";
    }
2.
Match rating: 67.53% · See similar code snippets
php logo
private function _getLikeRegex($string)
  {
    $regex = preg_quote($string, '/');
    $regex = preg_replace('/(?<!\\\\)%/', '.*?', $regex);
    $regex = str_replace('\\\\%', '\\%', $regex);
    return '/^' . $regex . '$/';
  }
3.
fortrabbit/datafilter · Basic.php
Match rating: 66.97% · See similar code snippets
php logo
public static function ruleRegex($regex)
    {
        $args = func_get_args();
        $regex = join(':', $args);

        /*not in format "/../<modifier>", "#..#<modifier>"  nor "~..~<modifier>" */
        if (!preg_match('/^([\/#~]).+\1[msugex]*$/', $regex)) {
            $regex = '/'. stripslashes($regex). '/';
        }

        return function ($input) use ($regex) {
            return preg_match($regex, $input);
        };
    }
4.
peakphp/framework · RouteExpression.php
Match rating: 66.79% · See similar code snippets
php logo
private function compile()
    {
        // replace pseudo {param}:type syntax to valid regex
        if (strpos($this->regex, '}:') !== false) {
            $this->regex = preg_replace('#\{([a-zA-Z0-9_-]+)\}:([a-z]+)#', '(?P<$1>:$2)', $this->expression);
            $this->regex = str_replace(
                array_keys($this->regexExpression),
                array_values($this->regexExpression),
                $this->regex
            );
        }

        // replace pseudo {param} syntax without type to valid regex
        if (strpos($this->regex, '}') !== false) {
            $this->regex = preg_replace('#\{([a-zA-Z_]+)\}#', '(?P<$1>[^\/]+)', $this->regex);
        }

        // allow optional trailing slash at the end
        if (substr($this->regex, -1) !== '/') {
            $this->regex .= '[\/]?';
        }
    }
5.
fuelphp/routing · Parameter.php
Match rating: 66.75% · See similar code snippets
php logo
public function getRegex()
	{
		$regex = '(?P<'.$this->name.'>'.$this->regex.')';

		if ($this->optional)
		{
			$regex = '(?:'.$regex.')?';
		}

		return $regex;
	}
6.
hunzhiwange/framework · Dispatch.php
Match rating: 66.41% · See similar code snippets
php logo
protected function prepareRegexForWildcard(string $regex): string
    {
        $regex = preg_quote($regex, '/');
        $regex = '/^'.str_replace('\*', '(\S*)', $regex).'$/';

        return $regex;
    }
7.
greg-md/php-router · RouteUtils.php
Match rating: 65.67% · See similar code snippets
php logo
private static function paramFetchRegex(string $regex): string
    {
        if ($regex === '*') {
            return '.+?';
        }

        $regex = Regex::disableGroups($regex);

        if ($regex[-1] == '+' or $regex[-1] == '*') {
            $regex .= '?';
        }

        return $regex;
    }
8.
dadajuice/zephyrus · Route.php
Match rating: 65.39% · See similar code snippets
php logo
private function getUriRegexFromParameters($uri, $params)
    {
        $regex = str_replace('/', '\/', $uri);
        foreach ($params as $param) {
            $regex = str_replace('{' . $param . '}', '([^\/]+)', $regex);
        }
        return $regex;
    }
9.
nicoSWD/php-rule-parser · Tokenizer.php
Match rating: 64.9% · See similar code snippets
php logo
private function getRegex(): string
    {
        if (!$this->regex) {
            $regex = [];

            foreach ($this->getQueue() as $token) {
                $regex[] = "(?<$token->class>$token->regex)";
            }

            $this->regex = '~(' . implode('|', $regex) . ')~As';
        }

        return $this->regex;
    }
10.
ncou/Chiron · Route.php
Match rating: 64.88% · See similar code snippets
php logo
private function sanitizeRequirement(string $key, string $regex): string
    {
        if ('' !== $regex && '^' === $regex[0]) {
            $regex = substr($regex, 1); // returns false for a single character
        }
        if ('$' === substr($regex, -1)) {
            $regex = substr($regex, 0, -1);
        }
        if ('' === $regex) {
            throw new InvalidArgumentException(sprintf('Routing requirement for "%s" cannot be empty.', $key));
        }

        return $regex;
    }