lastdragon-ru/glob-matcher
Composer 安装命令:
composer require lastdragon-ru/glob-matcher
包简介
Full-featured well-tested glob pattern parser and matcher: basic matching (`?`, `*`), globstar (`**`), extglob (`?(pattern-list)`, `*(pattern-list)`, `+(pattern-list)`, `@(pattern-list)`, `!(pattern-list)`), brace expansion (`{a,b,c}.txt`, `{1..3}.txt`, etc), dotglob, nocasematch, POSIX Named charac
README 文档
README
Full-featured well-tested glob pattern parser and matcher: basic matching (?, *), globstar (**), extglob (?(pattern-list), *(pattern-list), +(pattern-list), @(pattern-list), !(pattern-list)), brace expansion ({a,b,c}.txt, {1..3}.txt, etc), dotglob, nocasematch, POSIX Named character classes ([:alnum:], etc), POSIX Collating symbols ([.ch.], etc), POSIX Equivalence class expressions ([=a=], etc)1, and escaping2. Everything supported 😎
Requirements
| Requirement | Constraint | Supported by |
|---|---|---|
| PHP | ^8.5 |
HEAD ⋯ 11.0.0 |
^8.4 |
HEAD ⋯ 9.2.0 |
|
^8.3 |
10.3.0 ⋯ 9.2.0 |
Installation
composer require lastdragon-ru/glob-matcher
Usage
<?php declare(strict_types = 1); namespace LastDragon_ru\GlobMatcher\Docs\Examples; use LastDragon_ru\GlobMatcher\GlobMatcher; use LastDragon_ru\GlobMatcher\Options; use LastDragon_ru\LaraASP\Dev\App\Example; // Full-featured $fullGlob = new GlobMatcher('/**/{a,b,c}.txt'); Example::dump($fullGlob->match('/a.txt')); Example::dump($fullGlob->match('/a/b/c.txt')); Example::dump($fullGlob->match('/a/b/d.txt')); // Without `globstar` $noGlobstar = new GlobMatcher('/**/{a,b,c}.txt', new Options(globstar: false)); Example::dump($noGlobstar->match('/a.txt')); Example::dump($noGlobstar->match('/**/a.txt')); // Escaping $escaped = new GlobMatcher('/\\*.txt'); Example::dump(GlobMatcher::escape('/*.txt')); Example::dump($escaped->match('/a.txt')); Example::dump($escaped->match('/*.txt'));
The $fullGlob->match('/a.txt') is:
true
The $fullGlob->match('/a/b/c.txt') is:
true
The $fullGlob->match('/a/b/d.txt') is:
false
The $noGlobstar->match('/a.txt') is:
false
The $noGlobstar->match('/**/a.txt') is:
true
The GlobMatcher::escape('/*.txt') is:
"/\*.txt"
The $escaped->match('/a.txt') is:
false
The $escaped->match('/*.txt') is:
true
Globbing
The Glob is used internally by the GlobMatcher to parse the glob pattern(s). You can also use it if you, for example, need access to AST.
<?php declare(strict_types = 1); namespace LastDragon_ru\GlobMatcher\Docs\Examples; use LastDragon_ru\GlobMatcher\Glob\Glob; use LastDragon_ru\LaraASP\Dev\App\Example; $glob = new Glob('/**/**/?.txt'); Example::dump((string) $glob->regex); Example::dump($glob->node);
The (string) $glob->regex is:
"#^(?:/)(?:(?:(?<=^|/)(?:(?!\.)(?:(?=.))[^/]*?)(?:(?:/|$)|(?=/|$)))*?)(?:(?!\.)(?:(?=.)(?:[^/])(?:\.txt)))$#us"
The $glob->node is:
LastDragon_ru\GlobMatcher\Glob\Ast\Nodes\GlobNode {
+children: [
LastDragon_ru\GlobMatcher\Glob\Ast\Nodes\SegmentNode {},
LastDragon_ru\GlobMatcher\Glob\Ast\Nodes\GlobstarNode {
+count: 2
},
LastDragon_ru\GlobMatcher\Glob\Ast\Nodes\NameNode {
+children: [
LastDragon_ru\GlobMatcher\Glob\Ast\Nodes\QuestionNode {},
LastDragon_ru\GlobMatcher\Glob\Ast\Nodes\StringNode {
+string: ".txt"
},
]
},
]
}
Available options:
<?php declare(strict_types = 1); namespace LastDragon_ru\GlobMatcher\Glob; use LastDragon_ru\GlobMatcher\MatchMode; readonly class Options { public function __construct( /** * If set, the `**` will match all files and zero or more directories * and subdirectories. * * The same as `globstar`. */ public bool $globstar = true, /** * Enables extended globbing (`?(pattern-list)`, etc). * * The same as `extglob`. */ public bool $extended = true, /** * Filenames beginning with a dot are hidden and not matched by default * unless the glob begins with a dot or this option set to `true`. * * The same as `dotglob`. */ public bool $hidden = false, public MatchMode $matchMode = MatchMode::Match, /** * The same as `nocasematch`. */ public bool $matchCase = true, ) { // empty } }
Brace expansion
You can also expand braces without globbing:
<?php declare(strict_types = 1); namespace LastDragon_ru\GlobMatcher\Docs\Examples; use LastDragon_ru\GlobMatcher\BraceExpander\BraceExpander; use LastDragon_ru\LaraASP\Dev\App\Example; use function iterator_to_array; $expander = new BraceExpander('{a,{0..10..2},c}.txt'); Example::dump(iterator_to_array($expander)); Example::dump($expander->node);
The iterator_to_array($expander) is:
[
"a.txt",
"0.txt",
"2.txt",
"4.txt",
"6.txt",
"8.txt",
"10.txt",
"c.txt",
]
The $expander->node is:
LastDragon_ru\GlobMatcher\BraceExpander\Ast\Nodes\BraceExpansionNode {
+children: [
LastDragon_ru\GlobMatcher\BraceExpander\Ast\Nodes\SequenceNode {
+children: [
LastDragon_ru\GlobMatcher\BraceExpander\Ast\Nodes\BraceExpansionNode {
+children: [
LastDragon_ru\GlobMatcher\BraceExpander\Ast\Nodes\StringNode {
+string: "a"
},
]
},
LastDragon_ru\GlobMatcher\BraceExpander\Ast\Nodes\BraceExpansionNode {
+children: [
LastDragon_ru\GlobMatcher\BraceExpander\Ast\Nodes\IntegerSequenceNode {
+start: "0"
+end: "10"
+increment: 2
},
]
},
LastDragon_ru\GlobMatcher\BraceExpander\Ast\Nodes\BraceExpansionNode {
+children: [
LastDragon_ru\GlobMatcher\BraceExpander\Ast\Nodes\StringNode {
+string: "c"
},
]
},
]
},
LastDragon_ru\GlobMatcher\BraceExpander\Ast\Nodes\StringNode {
+string: ".txt"
},
]
}
Constraints
We are using PCRE to match and the lastdragon-ru/text-parser package to parse glob patterns. Both are limits encoding to UTF-8 only.
Path always checks as is. Unlike bash, there is no special processing of quotes/parentheses inside the pattern.
Only / allowed as a path separator. The \ used by Windows is not supported (it is used as an escape character).
The \ is always used as an escape character, so [\b] will be treated as [b] (\ is gone), [\\b] should be used instead.
The /, . and .. always match explicitly. Thus, the a/** will not match a, but will a/. Also, the / cannot be escaped and should not be inside the character class, extended pattern, etc. This means that, e.g. [a/b] will be parsed as [a, /, b] and not as characters a/b.
Gratitude
Huge thanks to micromatch and especially picomatch project for a vast set of tests of all features of glob.
Upgrading
Please follow Upgrade Guide.
Contributing
Please use the main repository to report issues, send pull requests, or ask questions.
Footnotes
-
Parsing only, PCRE limitation 🤷♂️ ↩
-
Except
/, see Constraints for more details. ↩
lastdragon-ru/glob-matcher 适用场景与选型建议
lastdragon-ru/glob-matcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「glob」 「glob pattern」 「glob matcher」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lastdragon-ru/glob-matcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lastdragon-ru/glob-matcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lastdragon-ru/glob-matcher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
???? Nette Finder: find files and directories with an intuitive API.
The PHP media library: media processing and MIME-Type/extension guessing.
Various iterators.
A Laravel package for the Repository Design Pattern.
Inbox pattern process implementation for your Laravel Applications
PHP Ant Glob
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-22