Returns a regex-compatible range from two numbers, min and max. Inspired by jonschlinkert/to-regex-range.
That's a good question. You normally would write something like if ($x > $min && $x < $max) { ... }
, right?
Say you want to build a glob like function yourself.
If your glob would support syntax like foo/{1..5}.md
and you plan to do the matching using a regex pattern.. Well, this library can convert 1..5
to a regex.
Via Composer
$ composer require hansott/range-regex
use HansOtt\RangeRegex\FactoryDefault;
use HansOtt\RangeRegex\Range;
$factory = new FactoryDefault();
$converter = $factory->getConverter();
$range = new Range(1, 3456);
$regex = sprintf('/^(%s)$/', $converter->toRegex($range));
// /^([1-9]|[1-9][0-9]|[1-9][0-9]{2}|[1-2][0-9]{3}|3[0-3][0-9]{2}|34[0-4][0-9]|345[0-6])$/
$matchesRegex = (bool) preg_match($regex, 0); // false
$matchesRegex = (bool) preg_match($regex, 2014); // true
$matchesRegex = (bool) preg_match($regex, 3457); // false
Please see CHANGELOG for more information what has changed recently.
$ composer test
Please see CONTRIBUTING and CONDUCT for details.
The MIT License (MIT). Please see License File for more information.