PHPackages                             mnavarrocarter/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. mnavarrocarter/path-to-regexp-php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mnavarrocarter/path-to-regexp-php
=================================

A PHP port of Path-To-Regex JS

1.0.0(6y ago)01.1k5MITPHPPHP &gt;=7.2

Since Feb 8Pushed 6y agoCompare

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

READMEChangelogDependencies (3)Versions (2)Used By (5)

MNC PathToRegExpPHP
===================

[](#mnc-pathtoregexpphp)

[![Actions Status](https://github.com/mnavarrocarter/path-to-regexp-php/workflows/CI/badge.svg)](https://github.com/mnavarrocarter/path-to-regexp-php/actions)

Turns an Express-style path string such as `/user/:name` into a regular expression, so it can be used in routing engines.

This is an Object Oriented port of the famous JS library `path-to-regexp`, used by Node's Express and other js frameworks.

The hard work of porting the JS library to PHP was done originally done by Gil Polguère. He did the hard part.

I just added the following features:

1. Bumped PHP to 7.2 minimum
2. Added type hints where possible
3. Removed all the arrays passed by reference and used objects to store that state instead.

Usage
-----

[](#usage)

```
use MNC\PathToRegExpPHP\PathRegExpFactory;

$pathRegex = PathRegExpFactory::create('/user/:name');
$result = $pathRegex->match('/user/john');
$result->getMatchedString();    // '/user/john'
$result->getValues();           // ['name' => 'john']
```

You have several flags you can pass as options of the `create` method:

- `PathRegExpFactory::CASE_SENSITIVE`: When passed the route will be treated as case sensitive.
- `PathRegExpFactory::STRICT`: When passed a slash is allowed to be trailing the path.
- `PathRegExpFactory::END`: When **not** passed the path will match at the beginning.

By default, the only flag enabled in the create method is `PathRegExpFactory::END`.

### 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.

```
use MNC\PathToRegExpPHP\PathRegExpFactory;

$pathRegex = PathRegExpFactory::create('/:foo/:bar');
$pathRegex->getParts()[0]->getName(); // 'foo'
$pathRegex->getParts()[1]->getName(); // 'bar'

$result = $pathRegex->match('/test/route');
$result->getMatchedString();    // '/test/route'
$result->getValues();           // ['foo' => 'test', 'bar' => '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 `.`).

```
use MNC\PathToRegExpPHP\PathRegExpFactory;

$pathRegex = PathRegExpFactory::create('/:foo/:bar?');

$result = $pathRegex->match('/test');
$result->getMatchedString();    // '/test'
$result->getValues();           // ['foo' => 'test', 'bar' => null]

$result = $pathRegex->match('/test/route');
$result->getMatchedString();    // '/test/route'
$result->getValues();           // ['foo' => 'test', 'bar' => '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.

```
use MNC\PathToRegExpPHP\PathRegExpFactory;

$pathRegex = PathRegExpFactory::create('/:foo*');

$result = $pathRegex->match('/');
$result->getMatchedString();    // '/'
$result->getValues();           // ['foo' => null];

$result = $pathRegex->match('/bar/baz');
$result->getMatchedString();    // '/bar/baz'
$result->getValues();           // ['foo' => '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.

```
use MNC\PathToRegExpPHP\PathRegExpFactory;

$pathRegex = PathRegExpFactory::create('/:foo+');

$pathRegex->match('/'); // Will throw NoMatchException

$result = $pathRegex->match('/bar/baz');
$result->getMatchedString();    // '/bar/baz'
$result->getValues();           // ['foo' => '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.

```
use MNC\PathToRegExpPHP\PathRegExpFactory;

$pathRegex = PathRegExpFactory::create('/:foo(\\d+)');

$result = $pathRegex->match('/123');
$result->getMatchedString();    // '/123'
$result->getValues();           // ['foo' => '123']

$pathRegex->match('/abc'); // Will throw a NoMatchException
```

#### 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.

```
use MNC\PathToRegExpPHP\PathRegExpFactory;

$pathRegex = PathRegExpFactory::create('/:foo/(.*)');

$result = $pathRegex->match('/test/route');
$result->getMatchedString();   // '/test/route'
$result->getValues();          // ['foo' => 'test', '0' => 'route']
```

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2327d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/79e7625b4370021decec6b5b24191d657cf1f0435181bc26791314317e71155a?d=identicon)[mnavarrocarter](/maintainers/mnavarrocarter)

---

Top Contributors

[![mnavarrocarter](https://avatars.githubusercontent.com/u/17072441?v=4)](https://github.com/mnavarrocarter "mnavarrocarter (4 commits)")

---

Tags

pathpath-to-regexpphpphp7regexproutingconvertroutingpathrouteMatchregexp

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[league/uri-components

URI components manipulation library

31937.4M88](/packages/league-uri-components)[akaunting/laravel-money

Currency formatting and conversion package for Laravel

7855.7M44](/packages/akaunting-laravel-money)[watson/active

Laravel helper for recognising the current route, controller and action

3263.7M14](/packages/watson-active)[matthiasmullie/path-converter

Relative path converter

10231.2M8](/packages/matthiasmullie-path-converter)[gabrielelana/byte-units

Library to parse, format and convert byte units

1682.3M20](/packages/gabrielelana-byte-units)[khill/php-duration

Converts between colon formatted time, human-readable time and seconds

1571.8M20](/packages/khill-php-duration)

PHPackages © 2026

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