Glob-like pattern matching and utilities.
- PHP >= 7.0
composer require phlak/glob
use PHLAK\Utilities\Glob;
new Glob($pattern);
// or
Glob::pattern($pattern);When instantiating a Glob object you must supply a $pattern string that may
contain one or more of the following special matching expressions.
?matches any single character*matches zero or more characters excluding/**matches zero or more characters including/[abc]matches a single character from the set (i.e.a,borc)[a-c]matches a single character in the range (i.e.a,borc)[^abc]matches any character not in the set (i.e. nota,borc)[^a-c]matches any character not in the range (i.e. nota,borc){foo,bar,baz}matches any pattern in the set (i.e.foo,barorbaz)
Test if a string matches the glob pattern.
Glob::pattern('*.txt')->match('foo.txt'); // true
Glob::pattern('*.txt')->match('foo.log'); // falseTest if a string starts with the glob pattern.
Glob::pattern('foo/*')->matchStart('foo/bar.txt'); // true
Glob::pattern('foo/*')->matchStart('bar/foo.txt'); // falseTest if a string ends with the glob pattern.
Glob::pattern('**.txt')->matchEnd('foo/bar.txt'); // true
Glob::pattern('**.txt')->matchEnd('foo/bar.log'); // falseTest if a string contains the glob pattern.
Glob::pattern('bar')->matchWithin('foo/bar/baz.txt'); // true
Glob::pattern('bar')->matchWithin('foo/baz/qux.txt'); // falseConvet the glob-like pattern to a regular expression pattern.
Glob::pattern('foo')->toRegex(); // Returns '#^foo$#'
Glob::pattern('foo/bar.txt')->toRegex(); // Returns '#^foo/bar\.txt$#'
Glob::pattern('file.{yml,yaml}')->toRegex(); // Returns '#^file\.(yml|yaml)$#'You can also control line anchors via the $options parameter.
Glob::pattern('foo')->toRegex(Glob::NO_ANCHORS); // Returns '#foo#'
Glob::pattern('foo')->toRegex(Glob::START_ANCHOR); // Returns '#^foo#'
Glob::pattern('foo')->toRegex(Glob::END_ANCHOR); // Returns '#foo$#'
Glob::pattern('foo')->toRegex(Glob::BOTH_ANCHORS); // Returns '#^foo$#'
Glob::pattern('foo')->toRegex(Glob::START_ANCHOR | Glob::END_ANCHOR); // Returns '#^foo$#'A list of changes can be found on the GitHub Releases page.
See the Common Issues page for a list of common issues and help in solving them.
For general help and support join our Spectrum Community or reach out on Twitter.
Please report bugs to the GitHub Issue Tracker.
This project is licensed under the MIT License.