ashleydawson / globtoregex
Function that converts a glob pattern into a regular expression, extracted from the Symfony Finder component
1.0.0
2019-06-24 08:51 UTC
Requires
- php: ^7.1
Requires (Dev)
- phpunit/phpunit: ^6.5
- symfony/finder: ^4.3
This package is auto-updated.
Last update: 2024-10-25 07:06:36 UTC
README
PHP function that converts a glob pattern into a regular expression, extracted from the Symfony Finder component.
Installation
To install via Composer, do the following:
$ composer req ashleydawson/globtoregex
Requirements are:
- PHP >= 7.1
Basic Usage
Basic usage of the function is as follows:
<?php require __DIR__.'/vendor/autoload.php'; $regex = \AshleyDawson\GlobToRegex\glob_to_regex('/**/*.txt'); echo $regex;
Where output is:
#^(?=[^\.])/(?:(?=[^\.])[^/]++/)*(?=[^\.])[^/]*\.txt$#
Usage
The following simple example returns a set of matched file paths:
<?php require __DIR__.'/vendor/autoload.php'; use function AshleyDawson\GlobToRegex\glob_to_regex; $paths = [ '/foo/bar/sample.txt', '/baz/biz/example.txt', '/fiz/boo/music.mp3', ]; // Find matches for the glob pattern `/**/*.txt` $regex = glob_to_regex('/**/*.txt'); $matches = array_filter($paths, function ($path) use ($regex) { return preg_match($regex, $path); }); print_r($matches);
Where output is:
Array
(
[0] => /foo/bar/sample.txt
[1] => /baz/biz/example.txt
)
Testing
To run the test suite, do the following:
$ vendor/bin/phpunit -c .