verbalexpressions / php-verbal-expressions
PHP Regular expressions made easy
Installs: 43 511
Dependents: 2
Suggesters: 0
Security: 0
Stars: 2 410
Watchers: 124
Forks: 144
Open Issues: 2
Type:project
Requires
- php: >=5.3
Requires (Dev)
- phpunit/phpunit: * >=4
- squizlabs/php_codesniffer: ^3.1
This package is not auto-updated.
Last update: 2024-11-17 14:43:03 UTC
README
PHPVerbalExpressions
- ported from VerbalExpressions
VerbalExpressions is a PHP library that helps to construct hard regular expressions.
Installation
The project supports Composer so you have to install Composer first, before project setup.
$ composer require verbalexpressions/php-verbal-expressions:dev-master
Examples
<?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.", "-");
More examples are available in the following files:
Business readable language expression definition
$definition = 'start, then "http", maybe "s", then "://", maybe "www.", anything but " ", end'; $regex = new VerbalExpressionsScenario($definition);
Methods list
For all the above method (except test
) you could use the VerbalExpressionsScenario
.
Other Implementations
You can see an up to date list of all ports on VerbalExpressions.github.io.
Building the project and running the tests
The project supports Composer so you have to install Composer first before project setup.
curl -sS https://getcomposer.org/installer | php
php composer.phar install --dev
ln -s vendor/phpunit/phpunit/phpunit.php phpunit
./phpunit