Laravel Packages

Glob-like File and Pattern Matching Utilities for PHP

Published
Glob-like File and Pattern Matching Utilities for PHP image

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:

// Returns '#^foo$#'
Pattern::make('foo')->toRegex();
 
// Returns '#^foo/bar\.txt$#'
Pattern::make('foo/bar.txt')->toRegex();
 
// Returns '#^file\.(yml|yaml)$#'
Pattern::make('file.{yml,yaml}')->toRegex();
 
// You can control line anchors as well
 
// Returns '#foo#'
Pattern::make('foo')->toRegex(Glob::NO_ANCHORS);
// Returns '#^foo#'
Pattern::make('foo')->toRegex(Glob::START_ANCHOR);
// Returns '#^foo$#'
Pattern::make('foo')->toRegex(Glob::BOTH_ANCHORS);

This package also includes file glob utilities to get a list of files matching a file glob pattern.

// Get a list of files in a directory (Returns a Symfony Finder Component)
Glob::in('**.txt', 'some/file/path');
 
Glob::matchStart('foo/*', 'foo/bar.txt'); // true
Glob::matchStart('foo/*', 'bar/foo.txt'); // false
Glob::matchEnd('**.txt', 'foo/bar.txt'); // true
Glob::matchEnd('**.txt', 'foo/bar.log'); // false
 
// Filter and reject array of filenames
 
// Returns ['foo.txt', 'foo/bar.txt']
Glob::filter('**.txt', [
'foo', 'foo.txt', 'bar.zip', 'foo/bar.png', 'foo/bar.txt',
]);
 
// Returns ['foo', 'bar.zip', 'foo/bar.png']
Glob::reject('**.txt', [
'foo', 'foo.txt', 'bar.zip', 'foo/bar.png', 'foo/bar.txt',
]);

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article