seborromeo / path-to-regex
A PHP port of the js library path-to-regexp.
Requires
- php: ^8.1
Requires (Dev)
- phpunit/phpunit: ^10.5
This package is auto-updated.
Last update: 2026-03-11 03:34:07 UTC
README
A PHP port of the JavaScript library path-to-regexp.
Turn a path string such as /user/:name into a regular expression.
Installation
composer require seborromeo/path-to-regex
Usage
use SeBorromeo\PathToRegex\PathToRegex;
Parameters
Parameters match arbitrary strings in a path by matching up to the end of the segment, or up to any proceeding tokens. They are defined by prefixing a colon to the parameter name (:foo). Parameter names can use any valid JavaScript identifier, or be double quoted to use other characters (:"param-name").
$fn = PathToRegex::match('/:foo/:bar'); $fn('/test/route'); /** [ * 'path' => '/test/route', * 'params' => [ 'foo' => 'test', 'bar' => 'route' ] * ] */
Wildcard
Wildcard parameters match one or more characters across multiple segments. They are defined the same way as regular parameters, but are prefixed with an asterisk (*foo).
$fn = PathToRegex::match('/*splat'); $fn('/bar/baz'); // ['path' => '/bar/baz', 'splat' => [ 'bar', 'baz' ]]
Optional
Braces can be used to define parts of the path that are optional.
$fn = PathToRegex::match('/users{/:id}/delete'); $fn('/users/delete'); // [ 'path' => '/users/delete', 'params' => []] $fn("/users/123/delete"); // [ 'path' => '/users/123/delete', 'params' => ['id' => '123']]
Match
The match function returns a function for matching strings against a path:
- path String,
TokenDataobject, or array of strings andTokenDataobjects. - options (optional) (Extends pathToRegex options)
- decode Function for decoding strings to params, or
falseto disable all processing. (default:decodeURIComponent)
- decode Function for decoding strings to params, or
$fn = PathToRegex::match('/:foo/:bar');
Please note: path-to-regex is intended for ordered data (e.g. paths, hosts). It can not handle arbitrarily ordered data (e.g. query strings, URL fragments, JSON, etc).
PathToRegex
The pathToRegex function returns the Regex for matching strings against paths, and an array of keys for understanding the Regex matches.
- path String,
TokenDataobject, or array of strings andTokenDataobjects. - options (optional) (See parse for more options)
- sensitive Regexp will be case sensitive. (default:
false) - end Validate the match reaches the end of the string. (default:
true) - delimiter The default delimiter for segments, e.g.
[^/]for:namedparameters. (default:'/') - trailing Allows optional trailing delimiter to match. (default:
true)
- sensitive Regexp will be case sensitive. (default:
['regex' => $regex, 'keys' => $keys] = PathToRegex::pathToRegex('/:foo/:bar'); preg_match($regex, '/foo/123', $matches); // $matches = ['/foo/123', '123']