glob

Glob pattern matcher supporting *, ?, **, character classes and brace alternation.

v1.0.0Text & stringsbundledimport "glob";
Overview Installation Documentation Examples

Overview

A zero-dependency glob pattern matcher for HolyPHP supporting *, ?, **, [abc] / [!abc] / [a-z] character classes, {a,b} brace alternation, and \ escaping for file paths and strings.

bundled — works offline zero dependencies wildcard pattern

Quick start

import "glob";

// Match single paths
$ok = glob_match("src/**/*.{hphp,json}", "src/utils/helper.hphp"); // true
$no = glob_match("src/*.hphp", "src/utils/helper.hphp");          // false

// Filter a list of paths
$files = [
    "README.md",
    "src/main.hphp",
    "src/lib/math.hphp",
    "tests/main_test.hphp",
    "build/output.bin"
];
$sources = glob_filter("src/**/*.hphp", $files);
// ["src/main.hphp", "src/lib/math.hphp"]

// Expand brace alternations
$patterns = glob_expand("img/{thumb,full}/*.{png,jpg}");
// ["img/thumb/*.png", "img/thumb/*.jpg", "img/full/*.png", "img/full/*.jpg"]

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install glob

This adds the library to hphp.yaml, installs it into .hphp/packages/glob/ and pins the exact version in hphp.lock:

require:
  glob: "^1.0"

Use it

import "glob";

glob is bundled with the compiler, so import "glob"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install glob          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install glob
Packageglob
Version1.0.0 (follows the compiler)
Entry fileglob.hphp
Sourcepackages/lib/glob ↗

Documentation

Pattern Syntax

SyntaxMeaning
*Matches zero or more characters except /
?Matches any single character except /
**Matches zero or more characters including /
**/Matches zero or more directory segments (e.g. **/a.txt matches a.txt and x/y/a.txt)
[abc]Matches any single character in the set (except /)
[a-z]Matches any single character in the ASCII range (except /)
[!abc] / [^abc]Matches any single character NOT in the set (except /)
{a,b,c}Brace alternation (supports nesting and multiple groups)
\xEscapes metacharacter x to match it literally

API Reference

glob_match(string $pattern, string $text): bool

Returns true if $text matches $pattern, otherwise false. Throws an Exception if $pattern has invalid syntax.

glob_filter(string $pattern, array $items): array

Filters $items (an array of strings) and returns a new re-indexed array containing only the elements that match $pattern. Validates $pattern even when $items is empty, and throws an Exception if any element in $items is not a string.

glob_expand(string $pattern): array

Expands {a,b} brace alternations in $pattern into an array of concrete glob patterns. Validates syntax and throws an Exception on malformed patterns.

glob_escape(string $text): string

Escapes all glob metacharacters (*, ?, [, ], {, }, \, ,) in $text so it can be matched literally.

glob_validate(string $pattern): bool

Validates $pattern syntax. Returns true if valid, or throws an Exception with a descriptive message if invalid.

Error Handling

All syntax and input errors throw Exception with a glob: prefix:

Examples

01Quick start

Bundled with the compiler — import "glob"; works offline in any file, or add it to a project with hphp install glob.

import "glob";

// Match single paths
$ok = glob_match("src/**/*.{hphp,json}", "src/utils/helper.hphp"); // true
$no = glob_match("src/*.hphp", "src/utils/helper.hphp");          // false

// Filter a list of paths
$files = [
    "README.md",
    "src/main.hphp",
    "src/lib/math.hphp",
    "tests/main_test.hphp",
    "build/output.bin"
];
$sources = glob_filter("src/**/*.hphp", $files);
// ["src/main.hphp", "src/lib/math.hphp"]

// Expand brace alternations
$patterns = glob_expand("img/{thumb,full}/*.{png,jpg}");
// ["img/thumb/*.png", "img/thumb/*.jpg", "img/full/*.png", "img/full/*.jpg"]
Try it
hphp install glob
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_glob.hphp.