• Stars
    star
    106
  • Rank 324,902 (Top 7 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Glob-like file and pattern matching utility.

Splat

Join our Community Become a Sponsor One-time Donation
Latest Stable Version Total Downloads License GitHub branch checks state


Glob-like file and pattern matching utility.

Requirements

Installation

Install Splat with Composer.

composer require phlak/splat

Then import the Glob or Pattern classes as needed.

use PHLAK\Splat\Glob;
use PHLAK\Splat\Pattern;

Patterns

Glob methods accept a $pattern as the first parameter. This can be a string or an instance of \PHLAK\Splat\Pattern.

$pattern = new Pattern(...);
$pattern = Pattern::make(...);

A pattern string 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 or c)
  • [a-c] matches a single character in the range (i.e. a, b or c)
  • [^abc] matches any character not in the set (i.e. not a, b or c)
  • [^a-c] matches any character not in the range (i.e. not a, b or c)
  • {foo,bar,baz} matches any pattern in the set (i.e. foo, bar or baz)
    • Sets may contain other matching patterns (i.e. {foo,ba[rz]})

Assertions

The following assertions can be use to assert that a string is followed by, or not followed by, another pattern.

  • (=foo) matches any string that also contains foo
  • (!foo) matches any string that does not also contain foo

For example, a pattern of *.tar(!.{gz,xz}) will match a string ending with .tar or .tar.bz but not tar.gz or tar.xz.

Converting Patterns To Regular Expressions

Glob patterns can be converted to a regular expression pattern.

Pattern::make('foo')->toRegex(); // Returns '#^foo$#'
Pattern::make('foo/bar.txt')->toRegex(); // Returns '#^foo/bar\.txt$#'
Pattern::make('file.{yml,yaml}')->toRegex(); // Returns '#^file\.(yml|yaml)$#'

You can control regular expression line anchors via the $options parameter.

Pattern::make('foo')->toRegex(Glob::NO_ANCHORS); // Returns '#foo#'
Pattern::make('foo')->toRegex(Glob::START_ANCHOR); // Returns '#^foo#'
Pattern::make('foo')->toRegex(Glob::END_ANCHOR); // Returns '#foo$#'
Pattern::make('foo')->toRegex(Glob::BOTH_ANCHORS); // Returns '#^foo$#'
Pattern::make('foo')->toRegex(Glob::START_ANCHOR | Glob::END_ANCHOR); // Returns '#^foo$#'

Pattern Character Escaping

Sometimes you may have characters in a string that shouldn't be treated as matching expression characters. In those situations you can escape any character by preceeding it with a backslash (\).

Pattern::make('What is happening\?');
Pattern::make('Wall-E \[2008\].mp4');

You may also escape glob pattern characters from a string programmatically with the Pattern::escape() method.

Pattern::escape('What is happening?'); // Returns 'What is happening\?'
Pattern::escape('*.{yml,yaml}'); // Returns '\*.\{yml\,yaml\}'
Pattern::escape('[Ss]pl*t.txt'); // Returns '\[Ss\]pl\*t.txt'

Methods

Files In

Get a list of files in a directory matching a glob pattern.

Glob::in('**.txt', '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 a glob pattern.

Glob::match('*.txt', 'foo.txt'); // true
Glob::match('*.txt', 'foo.log'); // false

Match Start

Test if a string starts with a glob pattern.

Glob::matchStart('foo/*', 'foo/bar.txt'); // true
Glob::matchStart('foo/*', 'bar/foo.txt'); // false

Match End

Test if a string ends with a glob pattern.

Glob::matchEnd('**.txt', 'foo/bar.txt'); // true
Glob::matchEnd('**.txt', 'foo/bar.log'); // false

Match Within

Test if a string contains a glob pattern.

Glob::matchWithin('bar', 'foo/bar/baz.txt'); // true
Glob::matchWithin('bar', 'foo/baz/qux.txt'); // false

Filter an Array (of Strings)

Filter an array of strings to values matching a glob pattern.

Glob::filter('**.txt', [
    '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 a glob pattern.

Glob::reject('**.txt', [
    'foo', 'foo.txt', 'bar.zip', 'foo/bar.png', 'foo/bar.txt',
]);

// Returns ['foo', 'bar.zip', 'foo/bar.png']

Changelog

A list of changes can be found on the GitHub Releases page.

Troubleshooting

For general help and support join our GitHub Discussion or reach out on Twitter.

Please report bugs to the GitHub Issue Tracker.

This project is licensed under the MIT License.

More Repositories

1

Twine

String manipulation, leveled up!
PHP
496
star
2

SemVer

Semantic versioning helper library for PHP
PHP
169
star
3

docker-mumble

Docker image for Mumble server.
Dockerfile
87
star
4

Uses

The hardware and software I use on a regular basis.
60
star
5

Config

PHP library for simple configuration management
PHP
49
star
6

Soxy

Bash script for starting and stopping a SOCKS proxy tunnel over SSH in Linux.
Shell
26
star
7

StrGen

PHP library for simple, secure random string generation (e.g. - passwords / salts).
PHP
19
star
8

presentation

Presentation template using reveal.js + Tailwind CSS
HTML
11
star
9

Colorizer

PHP library for generating persistantly unique colors from a string.
PHP
11
star
10

docker-openvpn

Docker image for OpenVPN client/server.
Dockerfile
11
star
11

laravelcollective-html-form-snippets

Sublime Text 3 snippets for LaravelCollective/html form elements.
10
star
12

docker-minecraft

Docker image for (vanilla) Minecraft server.
Dockerfile
9
star
13

lifx-client

PHP client library for the LIFX API (v1)
PHP
8
star
14

vueColorClock

The color changing clock.
HTML
8
star
15

jColorClock

A color changing clock written in JavaScript.
JavaScript
6
star
16

Chronometer

Measure the passing of time.
PHP
6
star
17

Stash

Simple PHP caching library
PHP
6
star
18

CloudDrop

Store and retrieve files in cloud storage providers (i.e. Dropbox, Google Drive, Microsoft OneDrive, Amazon Cloud Drive, etc.)
PHP
5
star
19

sublime-text

My Sublime Text configuration.
Python
5
star
20

docker-speedtest

Docker image for the Speedtest CLI
Dockerfile
5
star
21

laravelcollective-html-form-snippets-atom

Atom snippets for LaravelCollective HTML form elements.
4
star
22

docker-syncthing

Docker image for Syncthing client/node.
Dockerfile
4
star
23

SemVer-CLI

Command line tool for managing semantic versioning within a project.
PHP
4
star
24

Resume.php

[DEPRECATED] My resume as a PHP class.
PHP
4
star
25

chriskankiewicz.com

My internet homepage.
PHP
3
star
26

docker-soxy

Docker image for SOCKS proxy client.
Dockerfile
3
star
27

github-backup

Shell + Python script for backing up GitHub repositories to local git bundles.
Shell
3
star
28

dont-fear-the-regex

Crash course presentation on regular expressions.
HTML
3
star
29

docker-flexget

Docker image for Flexget client.
Dockerfile
3
star
30

docker-hangoutsbot

Docker image for Hangoutsbot client.
Dockerfile
3
star
31

Cafelias

Randomly generated coffee slugs
PHP
2
star
32

docker-valheim

Docker image for Valheim dedicated server.
Dockerfile
2
star
33

restic-init

Initialization script for the restic backup program.
Shell
2
star
34

dotfiles

My custom configuration files for Linux.
Shell
2
star
35

docker-youtube-dl

Docker image for youtube-dl
Dockerfile
2
star
36

docker-transmission

Docker image for Transmission Daemon.
Dockerfile
2
star
37

string-manipulation-leveled-up

String manipulation, leveled up! Twine presentation slides.
JavaScript
2
star
38

CodingStandards

A pre-defined set of coding standards for PHP CS Fixer.
PHP
2
star
39

adguard-home-compose

A docker-compose configuration for Adguard Home
Shell
2
star
40

inner-circle-bot

The Inner Circle chat bot.
PHP
2
star
41

docker-iperf

Docker image for iperf server.
Dockerfile
2
star
42

Collection

Lightweight collection library
PHP
2
star
43

xhtml-transitional-template

A basic XHTML compliant web page template.
JavaScript
2
star
44

blog.chriskankiewicz.com

Pelican repository for http://blog.chriskankiewicz.com
HTML
1
star
45

dev-tools

An opiniated set of development packages and tools.
PHP
1
star
46

linux-machine-id

Get the unique machine ID on Linux.
Shell
1
star
47

webauth_redirect

Drupal module for redirecting anonymous users trying to access /user or /admin to an ASU webauth login page.
PHP
1
star
48

discord-bot

PHP
1
star
49

syncthing-compose

A docker-compose configuration for Syncthing.
Makefile
1
star
50

docker-crashplan

Docker image for CrashPlan client.
Shell
1
star
51

twine-docs

Twine Documentation
Blade
1
star
52

ubuntu-setup.sh

[DEPRECATED] Interactive script to automate Ubuntu setup after installation.
Shell
1
star
53

phlak.net

Official PHLAK.NET homepage.
JavaScript
1
star
54

link-shortener

Custom link shortener app
PHP
1
star
55

PyBlackjack

Personal project to learn some Python, allows you to play Blackjack.
Python
1
star
56

docker-ssh-tunnel

Docker image for SSH tunnel.
Dockerfile
1
star