Splat is a PHP utility by Chris Kankiewicz that provides glob-like file and pattern matching. With this utility, you can convert patterns (see the README) into regular expressions using the provided Pattern
instance:
1// Returns '#^foo$#'
2Pattern::make('foo')->toRegex();
3
4// Returns '#^foo/bar\.txt$#'
5Pattern::make('foo/bar.txt')->toRegex();
6
7// Returns '#^file\.(yml|yaml)$#'
8Pattern::make('file.{yml,yaml}')->toRegex();
9
10// You can control line anchors as well
11
12// Returns '#foo#'
13Pattern::make('foo')->toRegex(Glob::NO_ANCHORS);
14// Returns '#^foo#'
15Pattern::make('foo')->toRegex(Glob::START_ANCHOR);
16// Returns '#^foo$#'
17Pattern::make('foo')->toRegex(Glob::BOTH_ANCHORS);
This package also includes file glob utilities to get a list of files matching a file glob pattern.
1// Get a list of files in a directory (Returns a Symfony Finder Component)
2Glob::in('**.txt', 'some/file/path');
3
4Glob::matchStart('foo/*', 'foo/bar.txt'); // true
5Glob::matchStart('foo/*', 'bar/foo.txt'); // false
6Glob::matchEnd('**.txt', 'foo/bar.txt'); // true
7Glob::matchEnd('**.txt', 'foo/bar.log'); // false
8
9// Filter and reject array of filenames
10
11// Returns ['foo.txt', 'foo/bar.txt']
12Glob::filter('**.txt', [
13 'foo', 'foo.txt', 'bar.zip', 'foo/bar.png', 'foo/bar.txt',
14]);
15
16// Returns ['foo', 'bar.zip', 'foo/bar.png']
17Glob::reject('**.txt', [
18 'foo', 'foo.txt', 'bar.zip', 'foo/bar.png', 'foo/bar.txt',
19]);
You can learn more about this package, get full installation instructions, and view the source code on GitHub.