concerto / text-expressions
文本表达式引擎库,用于搜索、替换和测试文本。
v0.4
2014-05-02 18:28 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: 4.*
This package is not auto-updated.
Last update: 2020-08-07 18:36:39 UTC
README
文本表达式引擎库,用于搜索、替换和测试文本。
安装
安装文本表达式的推荐方法是通过composer。
{ "require": { "concerto/text-expressions": "0.*" } }
使用方法
正则表达式
使用直观的面向对象(OOP)方式包装PHP的内部PCRE函数。
use Concerto\TextExpressions\RegularExpression as RegExp; // Does a string contain the words 'than' or 'then': $exp = new RegExp('\b(?<word>th[ae]n)\b', 'i'); $exp->test('not see much'); // > false $exp->test('they did then'); // > true $exp->execute('they did then')->replace('Found: $word')l // > Found: then
路径表达式
用于解析HTTP请求路径的表达式引擎。
use Concerto\TextExpressions\PathExpression as PathExp; // Quick and easy testing: $exp = new PathExp('/blog/<page>'); $exp->test('/blog/1'); // > true // Add regular expressions for validation: $exp = new PathExp('/blog/<page=[1-9][0-9]*>'); $exp->test('/blog/a'); // > false $exp->test('/blog/1'); // > true // Testing even more complex paths: $exp = new PathExp('/blog/<year=[0-9]{4}>/<month=0[1-9]|10|11|12>/rss'); $exp->test('/blog/2014/00/rss'); // > false $exp->test('/blog/2014/13/rss'); // > false $exp->test('/blog/2014/05/rss'); // > true // Extract or replace parameters: $exp = new PathExp('/blog/<year=[0-9]{4}>/<month=0[1-9]|10|11|12>/<article>'); $match = $exp->execute('/blog/2014/05/i-have-existed-since-the-dawn-of-time'); $match->article; // > i-have-existed-since-the-dawn-of-time $match->replace('/blog/$article'); // > /blog/i-have-existed-since-the-dawn-of-time
或者,您可以定义自己的词法分析器并设置一些默认表达式
use Concerto\TextExpressions\PathLexer; // Set default expression for page: $lexer = new PathLexer(); $lexer->setParameter('page', '[1-9][0-9]*'); // Using page without an expression: $exp = new PathExp('/blog/<page>', $lexer); $exp->test('/blog/a'); // > false $exp->test('/blog/1'); // > true
查询表达式
用于解析HTTP查询字符串的表达式引擎。目前,使用此库无法解析包含数组的查询字符串。
use Concerto\TextExpressions\QueryExpression as QueryExp; // Quick and easy testing: $exp = new QueryExp('page'); $exp->test('page=1'); // > true // The order of arguments does not matter: $exp = new QueryExp('first&second'); $exp->test('second&first'); // > true // Add regular expressions for validation: $exp = new QueryExp('<page=[1-9][0-9]*>'); $exp->test('page=a'); // > false $exp->test('page=1'); // > true // Testing even more complex paths: $exp = new QueryExp('<year=[0-9]{4}>&<month=0[1-9]|10|11|12>&feed=rss'); $exp->test('year=2014&month=00&feed=rss'); // > false $exp->test('year=2014&month=13&feed=rss'); // > false $exp->test('year=2014&month=05&feed=rss'); // > true // Extract or replace parameters: $exp = new QueryExp('<year=[0-9]{4}>&<month=0[1-9]|10|11|12>&article'); $match = $exp->execute('year=2014&month=05&article=i-have-existed-since-the-dawn-of-time'); $match->article; // > i-have-existed-since-the-dawn-of-time $match->replace('article=$article'); // > article=i-have-existed-since-the-dawn-of-time // Build a new query from the results: $match->build(); // > year=2014&month=05&article=i-have-existed-since-the-dawn-of-time
或者,您可以定义自己的词法分析器并设置一些默认表达式
use Concerto\TextExpressions\QeuryLexer; // Set default expression for page: $lexer = new QeuryLexer(); $lexer->setParameter('page', '[1-9][0-9]*'); // Using page without an expression: $exp = new QueryExp('page', $lexer); $exp->test('page=a'); // > false $exp->test('page=1'); // > true
变量表达式
模拟PHP内嵌变量语法。
use Concerto\TextExpressions\VariableExpression as VarExp; $exp = new VarExp('Take it easy {$name}!'); $exp->execute(['name' => 'human']); // > Take it easy human!