All Projects → gpolguere → path-to-regexp-php

gpolguere / path-to-regexp-php

Licence: MIT license
PHP port of https://github.com/pillarjs/path-to-regexp

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to path-to-regexp-php

node-match-path
Matches a URL against a path. Parameters, wildcards, RegExp.
Stars: ✭ 30 (+42.86%)
Mutual labels:  routing, path, route
Oniguruma
regular expression library
Stars: ✭ 1,643 (+7723.81%)
Mutual labels:  regexp, regular-expression
Orchestra
One language to be RegExp's Successor. Visually readable and rich, technically safe and extended, naturally scalable, advanced, and optimized
Stars: ✭ 103 (+390.48%)
Mutual labels:  regexp, regular-expression
Regexpu
A source code transpiler that enables the use of ES2015 Unicode regular expressions in ES5.
Stars: ✭ 201 (+857.14%)
Mutual labels:  regexp, regular-expression
Emoji Regex
A regular expression to match all Emoji-only symbols as per the Unicode Standard.
Stars: ✭ 1,134 (+5300%)
Mutual labels:  regexp, regular-expression
Hyperscan Java
Match tens of thousands of regular expressions within milliseconds - Java bindings for Intel's hyperscan 5
Stars: ✭ 66 (+214.29%)
Mutual labels:  regexp, regular-expression
Grex
A command-line tool and library for generating regular expressions from user-provided test cases
Stars: ✭ 4,847 (+22980.95%)
Mutual labels:  regexp, regular-expression
Regulex
🚧 Regular Expression Excited!
Stars: ✭ 4,877 (+23123.81%)
Mutual labels:  regexp, regular-expression
Regex For Regular Folk
🔍💪 Regular Expressions for Regular Folk — A visual, example-based introduction to RegEx [BETA]
Stars: ✭ 242 (+1052.38%)
Mutual labels:  regexp, regular-expression
vue-error-page
[NO LONGER MAINTAINED] Provides a wrapper for router-view that allows you to show error pages without changing the URL.
Stars: ✭ 52 (+147.62%)
Mutual labels:  routing, route
moar
Deterministic Regular Expressions with Backreferences
Stars: ✭ 19 (-9.52%)
Mutual labels:  regexp, regular-expression
Regexr
For composing regular expressions without the need for double-escaping inside strings.
Stars: ✭ 53 (+152.38%)
Mutual labels:  regexp, regular-expression
Commonregex
🍫 A collection of common regular expressions for Go
Stars: ✭ 733 (+3390.48%)
Mutual labels:  regexp, regular-expression
Regen
Tool to generate random strings from Go/RE2 regular expressions (Migrated to https://git.sr.ht/~nilium/regen)
Stars: ✭ 79 (+276.19%)
Mutual labels:  regexp, regular-expression
Onigmo
Onigmo is a regular expressions library forked from Oniguruma.
Stars: ✭ 536 (+2452.38%)
Mutual labels:  regexp, regular-expression
Regex Dos
👮 👊 RegEx Denial of Service (ReDos) Scanner
Stars: ✭ 143 (+580.95%)
Mutual labels:  regexp, regular-expression
Regexp2
A full-featured regex engine in pure Go based on the .NET engine
Stars: ✭ 389 (+1752.38%)
Mutual labels:  regexp, regular-expression
Picomatch
Blazing fast and accurate glob matcher written JavaScript, with no dependencies and full support for standard and extended Bash glob features, including braces, extglobs, POSIX brackets, and regular expressions.
Stars: ✭ 393 (+1771.43%)
Mutual labels:  regexp, regular-expression
String Replace Loader
Replace loader for Webpack
Stars: ✭ 205 (+876.19%)
Mutual labels:  regexp, regular-expression
Regaxor
A regular expression fuzzer.
Stars: ✭ 35 (+66.67%)
Mutual labels:  regexp, regular-expression

Path-to-RegExp

Turn an Express-style path string such as /user/:name into a regular expression.

This is a PHP port of the JS library component/path-to-regexp pillarjs/path-to-regexp without the support of JS native regexp (couldn't check the usage of the path).

Usage

require_once "PathToRegexp.php";

PathToRegexp::convert($path, $keys, $options);
  • path A string in the express format, an array of strings, or a regular expression.
  • keys An array to be populated with the keys present in the url.
  • options
    • options.sensitive When set to true the route will be case sensitive.
    • options.strict When set to true a slash is allowed to be trailing the path.
    • options.end When set to false the path will match at the beginning.
$keys = [];
$re = PathToRegexp::convert('/foo/:bar', $keys);
// $re = '/^\/foo\/([^\/]+?)\/?$/i'
// $keys = array(array("name" => 'bar', "delimiter" => '/', "repeat" => false, "optional" => false))

Parameters

The path has the ability to define parameters and automatically populate the keys array.

Named Parameters

Named parameters are defined by prefixing a colon to the parameter name (:foo). By default, this parameter will match up to the next path segment.

$re = PathToRegexp::convert('/:foo/:bar', $keys);
// $keys = array(array("name" => 'foo', ... ), array("name" => 'bar', ... ))

$matches = PathToRegexp::match($re, '/test/route');
// $matches = array('/test/route', 'test', 'route')

Suffixed Parameters

Optional

Parameters can be suffixed with a question mark (?) to make the entire parameter optional. This will also make any prefixed path delimiter optional (/ or .).

$re = PathToRegexp::convert('/:foo/:bar?', $keys);
// $keys = array(array("name" => 'foo', ... ), array("name" => 'bar', "delimiter" => '/', "optional" => true, "repeat" => false ))

$matches = PathToRegexp::match($re, '/test');
// $matches = array('/test', 'test', null)

$matches = PathToRegexp::match($re, '/test/route');
// $matches = array('/test', 'test', 'route')
Zero or more

Parameters can be suffixed with an asterisk (*) to denote a zero or more parameter match. The prefixed path delimiter is also taken into account for the match.

$re = PathToRegexp::convert('/:foo*', $keys);
// $keys = array(array("name" => 'foo', "delimiter" => '/', "optional" => true, "repeat" => true))

$matches = PathToRegexp::match($re, '/');
// $matches = array('/', null)

$matches = PathToRegexp::match($re, '/bar/baz');
// $matches = array('/bar/baz', 'bar/baz')
One or more

Parameters can be suffixed with a plus sign (+) to denote a one or more parameters match. The prefixed path delimiter is included in the match.

$re = PathToRegexp::convert('/:foo+', $keys);
// $keys = array(array("name" => 'foo', "delimiter" => '/', "optional" => false, "repeat" => true))

$matches = PathToRegexp::match($re, '/');
// $matches = null

$matches = PathToRegexp::match($re, '/bar/baz');
// $matches = array('/bar/baz', 'bar/baz')

Custom Match Parameters

All parameters can be provided a custom matching regexp and override the default. Please note: Backslashes need to be escaped in strings.

$re = PathToRegexp::convert('/:foo(\\d+)', $keys);
// $keys = array(array("name" => 'foo', ... ))

$matches = PathToRegexp::match($re, '/123');
// $matches = array('/123', '123')

$matches = PathToRegexp::match($re, '/abc');
// $matches = null

Unnamed Parameters

It is possible to write an unnamed parameter that is only a matching group. It works the same as a named parameter, except it will be numerically indexed.

$re = PathToRegexp::convert('/:foo/(.*)', $keys);
// $keys = array(array("name" => 'foo', ... ), array("name": '0', ... ))

$matches = PathToRegexp::match($re, '/test/route');
// $matches = array('/test/route', 'test', 'route')

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].