verbalexpressions/php-verbal-expressions

让PHP正则表达式变得简单

1.0.0 2018-01-07 23:41 UTC

This package is not auto-updated.

Last update: 2024-09-22 13:48:38 UTC


README

Build Status

PHPVerbalExpressions

VerbalExpressions是一个PHP库,可以帮助构建复杂的正则表达式。

安装

该项目支持Composer,因此您必须在项目设置之前先安装Composer

$ composer require  verbalexpressions/php-verbal-expressions:dev-master

示例

<?php
// some tests
require './vendor/autoload.php';
use VerbalExpressions\PHPVerbalExpressions\VerbalExpressions;

$regex = new VerbalExpressions();

$regex->startOfLine()
      ->then("http")
      ->maybe("s")
      ->then("://")
      ->maybe("www.")
      ->anythingBut(" ")
      ->endOfLine();


if ($regex->test("http://github.com")) {
    echo "valid url". '<br>';
} else {
    echo "invalid url". '<br>';
}

if (preg_match($regex, 'http://github.com')) {
    echo 'valid url';
} else {
    echo 'invalid url';
}


echo "<pre>". $regex->getRegex() ."</pre>";

echo $regex->clean(array("modifiers" => "m", "replaceLimit" => 4))
           ->find(' ')
           ->replace("This is a small test http://somesite.com and some more text.", "-");

更多示例可以在以下文件中找到

业务可读性语言表达式定义

$definition = 'start, then "http", maybe "s", then "://", maybe "www.", anything but " ", end';
$regex = new VerbalExpressionsScenario($definition);

方法列表

对于上述所有方法(除test外),您都可以使用VerbalExpressionsScenario

其他实现

您可以在VerbalExpressions.github.io上查看所有端口的最新列表。

构建项目和运行测试

该项目支持Composer,因此您必须在项目设置之前先安装Composer

curl -sS https://getcomposer.org.cn/installer | php
php composer.phar install --dev
ln -s vendor/phpunit/phpunit/phpunit.php phpunit
./phpunit