mcustiel / php-simple-regex
这是一个包含执行正则表达式并获取响应的实用工具的库
v1.1.0
2016-05-18 14:09 UTC
Requires
- php: >=5.5
Requires (Dev)
- markwilson/verbal-expressions-php: dev-master
- nordbjerg/verbal-expressions-php: dev-master
- phing/phing: >=2.11.0
- phploc/phploc: >=2.1.3
- phpmd/phpmd: >=2.2.3
- phpunit/phpunit: >=4.7.7
- sebastian/phpcpd: >=2.0.2
- selvinortiz/flux: dev-master
- squizlabs/php_codesniffer: >=2.3.3
- verbalexpressions/php-verbal-expressions: dev-master
This package is auto-updated.
Last update: 2024-09-19 09:21:42 UTC
README
这是什么
PhpSimpleRegex 是一个针对 PHP 的面向对象的正则表达式库。
此库允许在 PHP 中执行 preg_* 函数,并将结果作为对象使用,使 preg_* 函数的可测试性得到提高。PhpSimpleRegex 集成了 VerbalExpressions\PHPVerbalExpressions\VerbalExpressions、SelvinOrtiz\Utils\Flux\Flux 以及 MarkWilson\VerbalExpression,以允许在 PHP 中采用完全面向对象的方法处理正则表达式。
安装
Composer
如果您想直接访问此仓库,只需将以下内容添加到您的 composer.json 文件中即可
{ "require": { "mcustiel/php-simple-regex": "*" } }
或者直接下载发布版本并将其包含到您的路径中。
如何使用它?
此库提供了一个 外观 类,用于包装 PHP 中大部分的 preg_* 函数。您只需要创建此类的实例并调用所需的函数。
use Mcustiel\PhpSimpleRegex\Executor as RegexExecutor; $regexFacade = new RegexExecutor();
方法列表
- MatchResult getAllMatches(mixed $pattern, string $subject, integer $offset = 0)
- Match getOneMatch(mixed $pattern, string $subject, integer $offset = 0)
- boolean match(mixed $pattern, string $subject, integer $offset = 0)
- ReplaceResult replaceAndCount(mixed $pattern, string $replacement, mixed $subject, integer $limit = -1)
- mixed replace(mixed $pattern, string $replacement, mixed $subject, integer $limit = -1)
- mixed replaceCallback(mixed $pattern, callable $callback, mixed $subject, integer $limit = -1)
- ReplaceResult replaceCallbackAndCount(mixed $pattern, callable $callback, mixed $subject, integer $limit = -1)
- array split(mixed $pattern, string $string, integer $limit = -1, bool $returnOnlyNotEmpty = false, bool $captureOffset = false, bool $captureSubpatterns = false)
- array grep(mixed $pattern, array $input)
- array grepNotMatching(mixed $pattern, array $input)
对于每个方法,模式可以是字符串、Flux 对象或 PhpVerbalExpression 对象。
示例
getAllMatches
try { $result = $regexFacade->getAllMatches('/\d+/', 'ab12cd34ef56'); echo 'Number of matches: ' . $result->getMatchesCount() . PHP_EOL; // Prints 3 echo 'First match: ' . $result->getMatchAt(0)->getFullMatch() . PHP_EOL; // Prints 12 // Iterate over results foreach ($result as $index => $match) { echo "Match at index {$index} is " . $match->getFullMatch() . PHP_EOL; } } catch (\Exception $e) { echo 'An error occurred executing getAllMatches'; }
getOneMatch
try { $result = $regexFacade->getOneMatch('/\d+/', 'ab12cd34ef56'); if (!empty($result)) { echo 'Match: ' . $result->getFullMatch() . PHP_EOL; // Prints 12 } } catch (\Exception $e) { echo 'An error occurred executing getOneMatch'; }
match
try { if ($regexFacade->match('/\d+/', 'ab12cd34ef56')) { echo 'String matches pattern.'. PHP_EOL; } else { echo 'String does not match pattern.'. PHP_EOL; } } catch (\Exception $e) { echo 'An error occurred executing match'; }
replaceAndCount
try { // Subject can also be an array. $result = $this->executor->replaceAndCount('/\d+/', 'potato', 'ab12cd34ef56'); echo 'Number of replacements: ' . $result->getReplacements() . PHP_EOL; echo 'Replaced string: ' . $result->getResult() . PHP_EOL; } catch (\Exception $e) { echo 'An error occurred executing replaceAndCount'; }
replace
try { // Subject can also be a string. $result = $this->executor->replaceAndCount('/\d+/', 'potato', ['ab12cd34ef56', 'ab12cd78ef90']); echo 'Replaced strings: ' . print_r($result->getResult(), true) . PHP_EOL; } catch (\Exception $e) { echo 'An error occurred executing replace'; }
replaceCallback
try { // Subject can also be an array. $result = $this->executor->replaceCallback('/\d+/', function () { return 'potato'; }, 'ab12cd34ef56'); echo 'Replaced string: ' . $result->getResult() . PHP_EOL; } catch (\Exception $e) { echo 'An error occurred executing replaceCallback'; }
replaceCallbackAndCount
try { // Subject can also be an array. $result = $this->executor->replaceCallback('/\d+/', function () { return 'potato'; }, 'ab12cd34ef56'); echo 'Number of replacements: ' . $result->getReplacements() . PHP_EOL; echo 'Replaced string: ' . $result->getResult() . PHP_EOL; } catch (\Exception $e) { echo 'An error occurred executing replaceCallbackAndCount'; }