Thanks to visit codestin.com
Credit goes to github.com

Skip to content

PHLAK/Splat

Repository files navigation

Glob

License Join our Community Become a Sponsor One-time Donation Build Status StyleCI


Glob-like pattern matching and utilities.

Requirements

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 /
  • ** matches zero or more characters including /
  • [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)

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

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.

Sponsor this project

  •  

Contributors 6