PHPackages                             mcustiel/php-simple-regex - 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. mcustiel/php-simple-regex

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

mcustiel/php-simple-regex
=========================

This is a library with a set of utils to execute and get the response from regular expressions

v1.1.0(10y ago)513.1k↑83.3%1GPL-3.0+PHPPHP &gt;=5.5

Since Jul 19Pushed 8y ago3 watchersCompare

[ Source](https://github.com/mcustiel/php-simple-regex)[ Packagist](https://packagist.org/packages/mcustiel/php-simple-regex)[ RSS](/packages/mcustiel-php-simple-regex/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (4)Dependencies (10)Versions (5)Used By (0)

php-simple-regex
================

[](#php-simple-regex)

What is it
----------

[](#what-is-it)

PhpSimpleRegex is an object oriented regular expressions library for PHP.

This library allows to execute preg\_\* functions in PHP and use the results as objects, making the use of preg\_\* functions testeable. PhpSimpleRegex is integrated with [VerbalExpressions\\PHPVerbalExpressions\\VerbalExpressions](https://github.com/VerbalExpressions/PHPVerbalExpressions), [SelvinOrtiz\\Utils\\Flux\\Flux](https://github.com/selvinortiz/flux) and also [MarkWilson\\VerbalExpression](https://github.com/markwilson/VerbalExpressionsPhp) to allow a full Object Oriented approach to Regular Expressions in PHP.

[![Build Status](https://camo.githubusercontent.com/5ad2690ce1a6a7dee66ae99b986eaf34ae974bb64632c3427a26301bb45efe01/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6375737469656c2f7068702d73696d706c652d72656765782f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mcustiel/php-simple-regex/build-status/master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/f91c3ee1462e7d5670c2ad79ad51e1023210454d46879e8e2796e747fc65f247/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6375737469656c2f7068702d73696d706c652d72656765782f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mcustiel/php-simple-regex/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/89a4c0eb43da6fd790dba50d0ff2067820891df0e4b2a6029e96f5951f6566dd/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6375737469656c2f7068702d73696d706c652d72656765782f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mcustiel/php-simple-regex/?branch=master)

Installation
------------

[](#installation)

#### Composer:

[](#composer)

If you want to access directly to this repo, adding this to your composer.json should be enough:

```
{
    "require": {
        "mcustiel/php-simple-regex": "*"
    }
}
```

Or just download the release and include it in your path.

How to use it?
--------------

[](#how-to-use-it)

This library provides a *facade* class that wraps most of the preg\_\* functions from PHP. All you need to do is to create an instance of this class and call the methods you need.

```
use Mcustiel\PhpSimpleRegex\Executor as RegexExecutor;

$regexFacade = new RegexExecutor();
```

#### List of methods:

[](#list-of-methods)

- MatchResult **getAllMatches**(mixed $pattern, string $subject, integer $offset = 0)
- Match **getOneMatch**(mixed $pattern, string $subject, integer $offset = 0)
- boolean **match**(mixed $pattern, string $subject, integer $offset = 0)
- ReplaceResult **replaceAndCount**(mixed $pattern, string $replacement, mixed $subject, integer $limit = -1)
- mixed **replace**(mixed $pattern, string $replacement, mixed $subject, integer $limit = -1)
- mixed **replaceCallback**(mixed $pattern, callable $callback, mixed $subject, integer $limit = -1)
- ReplaceResult **replaceCallbackAndCount**(mixed $pattern, callable $callback, mixed $subject, integer $limit = -1)
- array **split**(mixed $pattern, string $string, integer $limit = -1, bool $returnOnlyNotEmpty = false, bool $captureOffset = false, bool $captureSubpatterns = false)
- array **grep**(mixed $pattern, array $input)
- array **grepNotMatching**(mixed $pattern, array $input)

For each method, the pattern can be a string, a Flux object, or a PhpVerbalExpression object.

Examples:
---------

[](#examples)

#### getAllMatches:

[](#getallmatches)

```
try {
    $result = $regexFacade->getAllMatches('/\d+/', 'ab12cd34ef56');
    echo 'Number of matches: ' . $result->getMatchesCount() . PHP_EOL; // Prints 3
    echo 'First match: ' . $result->getMatchAt(0)->getFullMatch() . PHP_EOL; // Prints 12

    // Iterate over results
    foreach ($result as $index => $match) {
        echo "Match at index {$index} is " . $match->getFullMatch() . PHP_EOL;
    }
} catch (\Exception $e) {
    echo 'An error occurred executing getAllMatches';
}
```

#### getOneMatch:

[](#getonematch)

```
try {
    $result = $regexFacade->getOneMatch('/\d+/', 'ab12cd34ef56');
    if (!empty($result)) {
        echo 'Match: ' . $result->getFullMatch() . PHP_EOL; // Prints 12
    }
} catch (\Exception $e) {
    echo 'An error occurred executing getOneMatch';
}
```

#### match:

[](#match)

```
try {
    if ($regexFacade->match('/\d+/', 'ab12cd34ef56')) {
        echo 'String matches pattern.'. PHP_EOL;
    } else {
        echo 'String does not match pattern.'. PHP_EOL;
    }
} catch (\Exception $e) {
    echo 'An error occurred executing match';
}
```

#### replaceAndCount:

[](#replaceandcount)

```
try {
    // Subject can also be an array.
    $result = $this->executor->replaceAndCount('/\d+/', 'potato', 'ab12cd34ef56');
    echo 'Number of replacements: ' . $result->getReplacements() . PHP_EOL;
    echo 'Replaced string: ' . $result->getResult() . PHP_EOL;
} catch (\Exception $e) {
    echo 'An error occurred executing replaceAndCount';
}
```

#### replace:

[](#replace)

```
try {
    // Subject can also be a string.
    $result = $this->executor->replaceAndCount('/\d+/', 'potato', ['ab12cd34ef56', 'ab12cd78ef90']);
    echo 'Replaced strings: ' . print_r($result->getResult(), true) . PHP_EOL;
} catch (\Exception $e) {
    echo 'An error occurred executing replace';
}
```

#### replaceCallback:

[](#replacecallback)

```
try {
    // Subject can also be an array.
    $result = $this->executor->replaceCallback('/\d+/', function () { return 'potato'; }, 'ab12cd34ef56');
    echo 'Replaced string: ' . $result->getResult() . PHP_EOL;
} catch (\Exception $e) {
    echo 'An error occurred executing replaceCallback';
}
```

#### replaceCallbackAndCount:

[](#replacecallbackandcount)

```
try {
    // Subject can also be an array.
    $result = $this->executor->replaceCallback('/\d+/', function () { return 'potato'; }, 'ab12cd34ef56');
    echo 'Number of replacements: ' . $result->getReplacements() . PHP_EOL;
    echo 'Replaced string: ' . $result->getResult() . PHP_EOL;
} catch (\Exception $e) {
    echo 'An error occurred executing replaceCallbackAndCount';
}
```

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity61

Established project with proven stability

 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

Every ~152 days

Total

3

Last Release

3699d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8d9b3ff93206038debfa1f16a11bbfc10fca9b2f4ddfdafa00e27365d290cf0d?d=identicon)[mcustiel](/maintainers/mcustiel)

---

Top Contributors

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

---

Tags

phppregregexregular-expressionlibraryregexOOPregular expressions

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/mcustiel-php-simple-regex/health.svg)

```
[![Health](https://phpackages.com/badges/mcustiel-php-simple-regex/health.svg)](https://phpackages.com/packages/mcustiel-php-simple-regex)
```

###  Alternatives

[composer/pcre

PCRE wrapping library that offers type-safe preg\_\* replacements.

707347.9M60](/packages/composer-pcre)[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k19.8M70](/packages/spatie-regex)[league/iso3166

ISO 3166-1 PHP Library

69938.4M141](/packages/league-iso3166)[dekor/php-array-table

PHP Library for printing associative arrays as text table (similar to mysql terminal console)

347.8M3](/packages/dekor-php-array-table)

PHPackages © 2026

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