Php 的内置preg_*函数需要一些奇怪的模式,例如通过引用传递变量以及将false或null值视为错误。 spatie/regex为preg_match 、 preg_match_all 、 preg_replace和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 是一家位于比利时安特卫普的网页设计机构。您可以在我们的网站上找到所有开源项目的概述。
我们投入了大量资源来创建一流的开源包。您可以通过购买我们的一款付费产品来支持我们。
我们非常感谢您从家乡寄给我们一张明信片,并注明您正在使用我们的哪种套餐。您可以在我们的联系页面上找到我们的地址。我们在虚拟明信片墙上发布所有收到的明信片。
您可以通过 Composer 安装该软件包:
composer require spatie/regex匹配主题上的模式。返回第一个匹配的MatchResult对象。
/ * *
* @ param string $ pattern
* @ param string $ subject
*
* @ return Spatie Regex MatchResult
* /
Regex:: match (string $ pattern , string $ subject ): MatchResultMatchResult::hasMatch(): bool检查模式是否与主题匹配。
Regex:: match ( ' /abc/ ' , ' abc ' )-> hasMatch (); // true
Regex:: match ( ' /def/ ' , ' abc ' )-> hasMatch (); // false MatchResult::result(): string返回进行的完整匹配。如果没有匹配则返回null 。
Regex:: match ( ' /abc/ ' , ' abc ' )-> result (); // 'abc'
Regex:: match ( ' /def/ ' , ' abc ' )-> result (); // null MatchResult::group(int $id): string返回捕获组的内容(索引从 1 开始)。如果组不存在,则引发RegexFailed异常。
Regex:: match ( ' /a(b)c/ ' , ' abc ' )-> group ( 1 ); // 'b'
Regex:: match ( ' /a(b)c/ ' , ' abc ' )-> group ( 2 ); // `RegexFailed` exception匹配主题上的模式。返回包含所有匹配项的MatchAllResult对象。
/ * *
* @ param string $ pattern
* @ param string $ subject
*
* @ return Spatie Regex MatchAllResult
* /
public static function matchAll( string $ pattern , string $ subject ): MatchAllResult MatchAllResult::hasMatch(): bool检查模式是否与主题匹配。
Regex:: matchAll ( ' /abc/ ' , ' abc ' )-> hasMatch (); // true
Regex:: matchAll ( ' /abc/ ' , ' abcabc ' )-> hasMatch (); // true
Regex:: matchAll ( ' /def/ ' , ' abc ' )-> hasMatch (); // false MatchAllResult::results(): array返回MatchResult对象的数组。
$ 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'替换主题中的模式。返回ReplaceResult对象。
/ * *
* @ 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(): mixedRegex:: replace ( ' /a/ ' , ' b ' , ' abc ' )-> result (); // 'bbc' Regex::replace也适用于可调用对象。可调用函数将接收一个MatchResult实例作为其参数。
Regex:: replace ( ' /a/ ' , function ( MatchResult $ matchResult ) {
return str_repeat ( $ matchResult -> result (), 2 );
}, ' abc ' )-> result (); // 'aabc'模式、替换和主题也可以是数组。在这些情况下, Regex::replace行为与preg_replace完全相同。
如果Regex方法中出现任何问题,则会引发RegexFailed异常。无需检查preg_last_error() 。
$ composer test 请参阅变更日志以了解有关最近更改内容的更多信息。
详细信息请参阅贡献。
请查看我们的安全政策,了解如何报告安全漏洞。
您可以自由使用这个软件包,但如果它进入您的生产环境,我们非常感谢您从您的家乡给我们寄一张明信片,注明您正在使用我们的哪个软件包。
我们的地址是:Spatie, Kruikstraat 22, 2018 安特卫普, 比利时。
我们在公司网站上发布所有收到的明信片。
麻省理工学院许可证 (MIT)。请参阅许可证文件以获取更多信息。