PHPackages                             valksor/php-functions-preg - 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. valksor/php-functions-preg

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

valksor/php-functions-preg
==========================

A PHP library providing enhanced regular expression (preg\_\*) functions with improved error handling and UTF-8 support

035PHPCI passing

Since Jan 24Pushed 3mo agoCompare

[ Source](https://github.com/valksor/php-functions-preg)[ Packagist](https://packagist.org/packages/valksor/php-functions-preg)[ RSS](/packages/valksor-php-functions-preg/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Valksor Functions: Preg
=======================

[](#valksor-functions-preg)

[![valksor](https://camo.githubusercontent.com/2af5a6a7e5f7da47cd0a924c8b00038f208f45f9b0d5be8d7a497a9808168187/68747470733a2f2f62616467656e2e6e65742f7374617469632f6f72672f76616c6b736f722f677265656e)](https://github.com/valksor)[![BSD-3-Clause](https://camo.githubusercontent.com/72547f8afb6b5ace804caebbf95c3bcbfc027ce9214777bc452f308f3165db01/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4253442d2d332d2d436c617573652d677265656e3f7374796c653d666c6174)](https://github.com/valksor/php-functions-preg/blob/master/LICENSE)[![Coverage Status](https://camo.githubusercontent.com/b7ca814a8ab9d6c33c4b1676c84698182e131a32b8d62a90499e8487305afebf/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f76616c6b736f722f7068702d66756e6374696f6e732d707265672f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/valksor/php-functions-preg?branch=master)[![php](https://camo.githubusercontent.com/77da2f7bbc049873edb2d1045a756d7a32e3ba50440a8e0e76a9109f62f0771b/68747470733a2f2f62616467656e2e6e65742f7374617469632f7068702f2533453d382e342f707572706c65)](https://www.php.net/releases/8.4/en.php)

A PHP library providing enhanced regular expression (preg\_\*) functions with improved error handling and UTF-8 support.

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

[](#installation)

Install the package via Composer:

```
composer require valksor/php-functions-preg
```

Requirements
------------

[](#requirements)

PHP 8.4 or higher

Usage
-----

[](#usage)

There are two ways to use this package: via the Functions class or by directly using the traits.

### Using the Functions Class

[](#using-the-functions-class)

The Functions class provides enhanced versions of PHP's preg\_\* functions with better error handling and UTF-8 support.

```
use Valksor\Functions\Preg\Functions;

// Create a new Functions instance
$preg = new Functions();

// Use enhanced preg_match
$isMatch = $preg->match('/pattern/', 'subject string', $matches);

// Use enhanced preg_replace
$replaced = $preg->replace('/pattern/', 'replacement', 'subject string');

// Use enhanced preg_split
$parts = $preg->split('/delimiter/', 'string to split');
```

### Using Traits Directly

[](#using-traits-directly)

Alternatively, you can use the traits directly in your own classes:

```
use Valksor\Functions\Preg\Traits\_Match;
use Valksor\Functions\Preg\Traits\_Replace;
use Valksor\Functions\Preg\Traits\_Split;

class MyClass
{
    // Import the traits you need
    use _Match;
    use _Replace;
    use _Split;

    public function doSomething(): void
    {
        // Use the methods directly
        $isMatch = $this->match('/pattern/', 'subject string', $matches);
        $replaced = $this->replace('/pattern/', 'replacement', 'subject string');
        $parts = $this->split('/delimiter/', 'string to split');
    }
}
```

Note that some traits may depend on other helper traits. For example, the `_Match` trait uses `_AddUtf8Modifier`, `_NewPregException`, and `_RemoveUtf8Modifier` internally. The Functions class handles these dependencies for you, but if you use the traits directly, you may need to include these helper traits as well.

### UTF-8 Support

[](#utf-8-support)

The library automatically handles UTF-8 patterns by trying to add or remove the UTF-8 modifier as needed:

```
// Will automatically try with and without UTF-8 modifier
$isMatch = $preg->match('/pattern/u', 'subject with UTF-8 characters');
```

### Error Handling

[](#error-handling)

The library provides improved error handling by throwing exceptions with detailed information when regex errors occur:

```
try {
    $result = $preg->match('/invalid[pattern/', 'subject');
} catch (\Exception $e) {
    // Handle the exception with detailed error information
    echo $e->getMessage();
}
```

### SkipErrorHandler

[](#skiperrorhandler)

The SkipErrorHandler executes a callback while capturing PHP errors and converting them to exceptions:

```
use Valksor\Functions\Preg\SkipErrorHandler;

try {
    $result = SkipErrorHandler::execute(function() {
        // Code that might trigger PHP errors
        $value = @file_get_contents('non-existent-file.txt');
        return $value;
    });
} catch (RuntimeException $e) {
    // Handle the error
    echo "Error: " . $e->getMessage();
}
```

Features
--------

[](#features)

For a complete list of all functions available in this package, see [Features](docs/features.md).

Contributing
------------

[](#contributing)

Contributions are welcome!

- Code style requirements (PSR-12)
- Testing requirements for PRs
- One feature per pull request
- Development setup instructions

To contribute to Preg functions:

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/new-preg-function`)
3. Implement your function following existing patterns
4. Add comprehensive tests including edge cases
5. Ensure all tests pass and code style is correct
6. Submit a pull request

Security
--------

[](#security)

If you discover any security-related issues, please email us at  instead of using the issue tracker.

Support
-------

[](#support)

- **Documentation**: [Full documentation](https://github.com/valksor/php-valksor)
- **Issues**: [GitHub Issues](https://github.com/valksor/php-valksor/issues) for bug reports and feature requests
- **Discussions**: [GitHub Discussions](https://github.com/orgs/valksor/discussions/categories/php-valksor) for questions and community support

Credits
-------

[](#credits)

- **[Original Author](https://github.com/valksor)** - Creator and maintainer
- **[All Contributors](https://github.com/valksor/php-valksor/graphs/contributors)** - Thank you to all who contributed
- **[PCRE Documentation](https://www.pcre.org/)** - Regular expression inspiration and standards
- **[Valksor Project](https://github.com/valksor)** - Part of the larger Valksor PHP ecosystem

License
-------

[](#license)

This package is licensed under the [BSD-3-Clause License](LICENSE).

About Valksor
-------------

[](#about-valksor)

This package is part of the [valksor/php-valksor](https://github.com/valksor/php-valksor) project - a comprehensive PHP library and Symfony bundle that provides a collection of utilities, components, and integrations for Symfony applications.

The main project includes:

- Various utility functions and components
- Doctrine ORM tools and extensions
- Symfony bundle for easy configuration
- And much more

If you find this Preg component useful, you might want to check out the full Valksor project for additional tools and utilities that can enhance your Symfony application development.

To install the complete package:

```
composer require valksor/php-valksor
```

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance53

Moderate activity, may be stable

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity12

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/c4db85e32518c5a6caa2fd625032a2b016ef42d60cf8a101c165cc5c0048b221?d=identicon)[k0d3r1s](/maintainers/k0d3r1s)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/valksor-php-functions-preg/health.svg)

```
[![Health](https://phpackages.com/badges/valksor-php-functions-preg/health.svg)](https://phpackages.com/packages/valksor-php-functions-preg)
```

###  Alternatives

[knplabs/knp-components

Knplabs component library

77443.6M46](/packages/knplabs-knp-components)[bangnokia/laravel-serve-livereload

Bring livereload to laravel artisan serve command

1008.9k](/packages/bangnokia-laravel-serve-livereload)[markbaker/phpgeodetic

Geodetic library

2013.0k](/packages/markbaker-phpgeodetic)[justintadlock/hybrid-carbon

God-like post featured image script.

202.5k](/packages/justintadlock-hybrid-carbon)[anlutro/menu

Dynamic menu builder.

191.7k1](/packages/anlutro-menu)

PHPackages © 2026

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