Migrate to T-Regx

Because CleanRegex is built on top of SafeRegex, we get protection against warnings, compile errors, notices, fatal errors and magic values for free!

Additionally, CleanRegex provides clean API for regular expressions, as well as solving more complicated issues with PHP regex (like eliminating false positives, validating groups) and including features utterly missing in PHP: Prepared patterns, offsets while replacing, in-place replacement, composite patterns, built-alteration and more, as well as removing uncertainty with false negatives and false positives.

Entry points#

We have multiple entry points, each with its own use case:

  • Standard functions - Pattern::of('\d+') or pattern('\d+')
  • Compatibility legacy - Pattern::pcre('/\d+/') to use vanilla patterns
  • Prepared patterns - to safely use user data in patterns (see Prepared Patterns)

Class method style:#

use TRegx\CleanRegex\Pattern;
Pattern::of('[A-Z][a-z]+')->test($subject);

Global function style:#

pattern('/[A-Z]#[a-z]+')->test($subject);

Prepared patterns:#

use TRegx\CleanRegex\Pattern;
Pattern::inject('^\d+:@$', [$_GET['user']])->test('14:mark'); // true, if $_GET['user'] == 'mark'

Automatic delimiters#

Notice, that these patterns are not delimited. Smart T-Regx will conveniently add them for you, if you use Pattern::of() or pattern(). You can add flags to your regular expression by passing a second argument.

pattern('[A-Z]+', 'im')->test('Hello');

To learn more, go to Automatic Delimiters.

Automatic vs. deliberate delimiters#

If you use pattern() or Pattern::of() with a delimited pattern, like so:

pattern('/[A-Z]/')->test('/A/');

...then / characters will simply be treated literally:

pattern('/[A-Z]/')->match('/A/')->first();
'/A/'

Old-school patterns#

Of course, you can still use delimited patterns with flags, with Pattern::pcre(). Both versions are equal with each other.

Pattern::pcre('/[A-Z]+/im')->test($subject);

Prepared Patterns#

To safely handle unsafe date or user input, see Prepared Patterns.

Last updated on