spatie/regex
Composer 安装命令:
composer require spatie/regex
包简介
A sane interface for php's built in preg_* functions
README 文档
README
Php's built in preg_* functions require some odd patterns like passing variables by reference and treating false or null values as errors. spatie/regex provides a cleaner interface for preg_match, preg_match_all, preg_replace and preg_replace_callback.
use Spatie\Regex\Regex; // Using `match` Regex::match('/a/', 'abc'); // `MatchResult` object Regex::match('/a/', 'abc')->hasMatch(); // true Regex::match('/a/', 'abc')->result(); // 'a' // Capturing groups with `match` Regex::match('/a(b)/', 'abc')->result(); // 'ab' Regex::match('/a(b)/', 'abc')->group(1); // 'b' // Setting defaults Regex::match('/a(b)/', 'xyz')->resultOr('default'); // 'default' Regex::match('/a(b)/', 'xyz')->groupOr(1, 'default'); // 'default' // Using `matchAll` Regex::matchAll('/a/', 'abcabc')->hasMatch(); // true Regex::matchAll('/a/', 'abcabc')->results(); // Array of `MatchResult` objects // Using replace Regex::replace('/a/', 'b', 'abc')->result(); // 'bbc'; Regex::replace('/a/', function (MatchResult $result) { return $result->result() . 'Hello!'; }, 'abc')->result(); // 'aHello!bc';
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/regex
Usage
Matching a pattern once
Matches a pattern on a subject. Returns a MatchResult object for the first match.
/** * @param string $pattern * @param string $subject * * @return \Spatie\Regex\MatchResult */ Regex::match(string $pattern, string $subject): MatchResult
MatchResult::hasMatch(): bool
Checks if the pattern matches the subject.
Regex::match('/abc/', 'abc')->hasMatch(); // true Regex::match('/def/', 'abc')->hasMatch(); // false
MatchResult::result(): string
Return the full match that was made. Returns null if no match was made.
Regex::match('/abc/', 'abc')->result(); // 'abc' Regex::match('/def/', 'abc')->result(); // null
MatchResult::group(int $id): string
Return the contents of a captured group (with a 1-based index). Throws a RegexFailed exception if the group doesn't exist.
Regex::match('/a(b)c/', 'abc')->group(1); // 'b' Regex::match('/a(b)c/', 'abc')->group(2); // `RegexFailed` exception
Matching all occurences of a pattern
Matches a pattern on a subject. Returns a MatchAllResult object containing all matches.
/** * @param string $pattern * @param string $subject * * @return \Spatie\Regex\MatchAllResult */ public static function matchAll(string $pattern, string $subject): MatchAllResult
MatchAllResult::hasMatch(): bool
Checks if the pattern matches the subject.
Regex::matchAll('/abc/', 'abc')->hasMatch(); // true Regex::matchAll('/abc/', 'abcabc')->hasMatch(); // true Regex::matchAll('/def/', 'abc')->hasMatch(); // false
MatchAllResult::results(): array
Returns an array of MatchResult objects.
$results = Regex::matchAll('/ab([a-z])/', 'abcabd')->results(); $results[0]->result(); // 'abc' $results[0]->group(1); // 'c' $results[1]->result(); // 'abd' $results[1]->group(1); // 'd'
Replacing a pattern in a subject
Replaces a pattern in a subject. Returns a ReplaceResult object.
/** * @param string|array $pattern * @param string|array|callable $replacement * @param string|array $subject * @param int $limit * * @return \Spatie\Regex\ReplaceResult */ public static function replace($pattern, $replacement, $subject, $limit = -1): ReplaceResult
ReplaceResult::result(): mixed
Regex::replace('/a/', 'b', 'abc')->result(); // 'bbc'
Regex::replace also works with callables. The callable will receive a MatchResult instance as it's argument.
Regex::replace('/a/', function (MatchResult $matchResult) { return str_repeat($matchResult->result(), 2); }, 'abc')->result(); // 'aabc'
Patterns, replacements and subjects can also be arrays. Regex::replace behaves exactly like preg_replace in those instances.
Error handling
If anything goes wrong in a Regex method, a RegexFailed exception gets thrown. No need for checking preg_last_error().
Testing
$ composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Postcardware
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards on our company website.
Credits
License
The MIT License (MIT). Please see License File for more information.
spatie/regex 适用场景与选型建议
spatie/regex 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.75M 次下载、GitHub Stars 达 1.12k, 最近一次更新时间为 2016 年 08 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「expression」 「regex」 「regular」 「expressions」 「spatie」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spatie/regex 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spatie/regex 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spatie/regex 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Business rules engine tools.
Advanced TYPO3 redirect management for sys_redirect with regex and host/language-aware matching, CSV/.htaccess import/export, categories and priority, automatic slug redirect workflows, plus hit/log analytics.
Adds PHP's preg_replace function as a Twig filter.
Convert Regular Expressions into text, for testing
Condition Builder for logical expressions
Fluent regular expressions in PHP.
统计信息
- 总下载量: 19.75M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1133
- 点击次数: 36
- 依赖项目数: 57
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2016-08-17