Skip to main content

Release 0.9.5 - Alternation in prepared patterns!

This release brings alternation in prepared patterns!

Up to this point, there was no reasonable way to create a pattern from a variable number of inputs, for example you allow your users to input 0, 1 or more tags, which later should be used in a pattern. In procedural world, probably array_map() with preg::quote() would do the job, but wait! You don't have to code, it's already here:

Pattern::bind('^user:@id/findBy:@tags/all$', [
'id' => $user->id,
'tags' => $_GET['tags']

In other words:

Pattern::bind('My tag is: "@tags"', ['tags' => ['one', 'two', 'three']]);

creates a pattern:

/My tag is: "(one|two|three)"/

Rest assured:

  • the values are quoted with preg::quote(), to protect you from malicious code
  • the group is non-capturing (use 'My tag is: "(@tags)"' for a capturing group, to be used with group())

The alternation is really smart too - if you use i or u flag, T-Regx will perform certain optimization, for example:

Pattern::inject('Find: @ :)', [['foo', 'bar', 'FOO']], 'i');

then it wil collapse foo and FOO, since i flag is used:

/Find: (foo|bar) :)/

That's it in this release! Stay tuned :)