PHPackages                             gpolguere/path-to-regexp-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. gpolguere/path-to-regexp-php

ActiveLibrary

gpolguere/path-to-regexp-php
============================

PHP port of https://github.com/component/path-to-regexp

2211.5k—0%5[1 issues](https://github.com/gpolguere/path-to-regexp-php/issues)2PHP

Since Dec 12Pushed 7y ago3 watchersCompare

[ Source](https://github.com/gpolguere/path-to-regexp-php)[ Packagist](https://packagist.org/packages/gpolguere/path-to-regexp-php)[ RSS](/packages/gpolguere-path-to-regexp-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (2)

Path-to-RegExp
==============

[](#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](https://github.com/component/path-to-regexp) [pillarjs/path-to-regexp](https://github.com/pillarjs/path-to-regexp) **without the support of JS native regexp** (couldn't check the usage of the path).

Usage
-----

[](#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

[](#parameters)

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

#### Named Parameters

[](#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

[](#suffixed-parameters)

##### Optional

[](#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

[](#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

[](#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

[](#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

[](#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
-------

[](#license)

MIT

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 Bus Factor1

Top contributor holds 91.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/279738?v=4)[Gil Polguère](/maintainers/gpolguere)[@gpolguere](https://github.com/gpolguere)

---

Top Contributors

[![gpolguere](https://avatars.githubusercontent.com/u/279738?v=4)](https://github.com/gpolguere "gpolguere (11 commits)")[![artyuum](https://avatars.githubusercontent.com/u/17199757?v=4)](https://github.com/artyuum "artyuum (1 commits)")

---

Tags

pathphpregexpregexp-special-charactersregular-expressionrouterouting

### Embed Badge

![Health badge](/badges/gpolguere-path-to-regexp-php/health.svg)

```
[![Health](https://phpackages.com/badges/gpolguere-path-to-regexp-php/health.svg)](https://phpackages.com/packages/gpolguere-path-to-regexp-php)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
