Diogo Peralta Cordeiro 88c194c013 | ||
---|---|---|
.. | ||
src | ||
LICENSE | ||
Makefile | ||
README.md | ||
composer.json | ||
psalm-baseline.xml |
README.md
Glob-like file and pattern matching utility.
Requirements
- PHP >= 7.0
Installation
composer require phlak/glob
Usage
Initialization
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.
Matching Expressions
?
matches any single character*
matches zero or more characters excluding/
(\
on Windows)**
matches zero or more characters including/
(\
on Windows)[abc]
matches a single character from the set (i.e.a
,b
orc
)[a-c]
matches a single character in the range (i.e.a
,b
orc
)[^abc]
matches any character not in the set (i.e. nota
,b
orc
)[^a-c]
matches any character not in the range (i.e. nota
,b
orc
){foo,bar,baz}
matches any pattern in the set (i.e.foo
,bar
orbaz
)
Files In
Get a list of files in a directory by a glob pattern.
Glob::pattern('**.txt')->in('some/file/path');
Returns a Symfony Finder Component
containing the files matching the glob pattern within the specified directory
(e.g. foo.txt
, foo/bar.txt
, foo/bar/baz.txt
, etc.).
Exact Match
Test if a string matches the glob pattern.
Glob::pattern('*.txt')->match('foo.txt'); // true
Glob::pattern('*.txt')->match('foo.log'); // false
Match Start
Test if a string starts with the glob pattern.
Glob::pattern('foo/*')->matchStart('foo/bar.txt'); // true
Glob::pattern('foo/*')->matchStart('bar/foo.txt'); // false
Match End
Test if a string ends with the glob pattern.
Glob::pattern('**.txt')->matchEnd('foo/bar.txt'); // true
Glob::pattern('**.txt')->matchEnd('foo/bar.log'); // false
Match Within
Test if a string contains the glob pattern.
Glob::pattern('bar')->matchWithin('foo/bar/baz.txt'); // true
Glob::pattern('bar')->matchWithin('foo/baz/qux.txt'); // false
Filter an Array (of Strings)
Filter an array of strings to values matching the glob pattern.
Glob::pattern('**.txt')->filter([
'foo', 'foo.txt', 'bar.zip', 'foo/bar.png', 'foo/bar.txt',
]);
// Returns ['foo.txt', 'foo/bar.txt']
Reject an Array (of Strings)
Filter an array of strings to values not matching the glob pattern.
Glob::pattern('**.txt')->reject([
'foo', 'foo.txt', 'bar.zip', 'foo/bar.png', 'foo/bar.txt',
]);
// Returns ['foo', 'bar.zip', 'foo/bar.png']
To Regular Expression
Convet 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$#'
Changelog
A list of changes can be found on the GitHub Releases page.
Troubleshooting
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.
Copyright
This project is licensed under the MIT License.