PHP Regular expressions made easy

Overview

Build Status

PHPVerbalExpressions

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

Name Description Usage
add add values to the expression add('abc')
startOfLine mark expression with ^ startOfLine(false)
endOfLine mark the expression with $ endOfLine()
then add a string to the expression add('foo')
find alias for then find('foo')
maybe define a string that might appear once or not maybe('.com')
anything accept any string anything()
anythingBut accept any string but the specified char anythingBut(',')
something accept any non-empty string something()
somethingBut anything non-empty except for these chars somethingBut('a')
replace shorthand for preg_replace() replace($source, $val)
lineBreak match \r \n lineBreak()
br shorthand for lineBreak br()
tab match tabs \t tab()
word match \w+ word()
anyOf any of the listed chars anyOf('abc')
any shorthand for anyOf any('abc')
range adds a range to the expression range(a,z,0,9)
withAnyCase match case default case sensitive withAnyCase()
stopAtFirst toggles the g modifiers stopAtFirst()
addModifier add a modifier addModifier('g')
removeModifier remove a mofier removeModifier('g')
searchOneLine Toggles m modifier searchOneLine()
multiple adds the multiple modifier multiple('*')
_or wraps the expression in an or with the provided value _or('bar')
limit adds char limit limit(1,3)
test performs a preg_match test('[email protected]')

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
Comments
  • Support for PHP 5.3-5.5

    Support for PHP 5.3-5.5

    I was wondering if support for 5.3 to 5.5 would be an option, if the slow func_get_args would be just a different implementation of the single method, provided in another class.

    A (new) abstract class would use: protected function range(array $args) The existing class would just call parent::range($args) A new class for older php would use parent::range(func_get_args())

    That would allow older versions to work without negatively impacting the performance of newer versions of php and the change would not break current code. Tests would need to be expanded to check that both range-implementations work the same if covering the single new line is desired.

    If desired I'll write the required code, I'd like to have a 5.3 compatible version in any case.

    opened by Idrinth 4
  • add VerbalExpressionScenario

    add VerbalExpressionScenario

    I have added the possibility to define the expression via a business readable language.

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

    I have also updated the README with a list of the VerbalExpressions methods.

    opened by niklongstone 4
  • Enabling continuous integration via Travis

    Enabling continuous integration via Travis

    I've swapped out the unit test for a travis test :D

    The readme.md image refers to your own repo, so you'll need to enable it on travis-ci.org

    It works though (see mine): Build Status

    opened by SignpostMarv 3
  • Introducing Composer and PHPUnit support

    Introducing Composer and PHPUnit support

    In order to install this lib with Composer I have re-structured the project. Besides I added PHPUnit (installed by Composer) and wrote the very first testcase (yet to be extended) as showcase.

    If this PR is accepted I will add more testcases.

    In addition I fixed the return value of VerbalExpressions. Originally int was returned but it should be bool.

    opened by frastel 3
  • Providing a tagged version?

    Providing a tagged version?

    I'm wondering if the library is by now stable enough to be given a tag - likely 1.0.0 - so it's less risky to depend on this library. Having a version just makes it way easier to tie down a specific api.

    opened by Idrinth 1
  • Increased consistency

    Increased consistency

    Hello,

    I propose these changes to increase consistency:

    • Always use "@access public" if a method is public
    • Always start the additional description text of a method with an upper case letter
    opened by chriskonnertz 1
  • Example+Readme psr2 compliance & moving dependencies

    Example+Readme psr2 compliance & moving dependencies

    Moved squizlabs/php_codesniffer to require-dev, since there is zero need for it in a production enviroment.

    Adjusted the examples in Example.php and the Readme.md to comply with PSR2, since the whole project has defined it as it's desired standard.

    opened by Idrinth 1
  • Tests and autoloading updates, use variadic parameters

    Tests and autoloading updates, use variadic parameters

    At first I wasn't be able to run tests, so I did some changes in that field, accordingly to best practices. I also switched from user_func_args() to variadic args, which is several times faster as there is no function call overhead. I have described all my actions in commit log.

    opened by xZero707 1
  • query regarding getRegex testing

    query regarding getRegex testing

    I've noticed that JSVerbalExpressions & PHPVerbalExpressions don't produce the same output for the same input; Which of the VerbalExpressions projects is considered the "master" to test against?

    example: input: regex->startOfLine()->range(0, 9, 'a', 'z', 'A', 'Z')->multiple('')

    $js = '/^[0-9a-zA-Z](?:)*/gm';
    
    $php = '/^[0-9a-zA-Z]+/m';
    

    (variables edited to clarify language)

    opened by SignpostMarv 1
  • PSR-2 + enhance phpdoc

    PSR-2 + enhance phpdoc

    | Q | A | | --- | --- | | Bug fix? | no | | Enhancement? | yes | | New feature? | no | | BC breaks? | no | | Deprecations? | no | | Tests pass? | yes |

    opened by ghost 1
  • Inform in composer.json the minimum PHP version

    Inform in composer.json the minimum PHP version

    Hi, it would be important to describe in composer.json which PHP versions is minimum for using this package (as described on http://getcomposer.org/doc/02-libraries.md#platform-packages)

    Example:

    {
        // ...
        "require": {
            // ...
            "php": ">=5.2"
        }
    }
    
    opened by rogeriopradoj 1
  • anythingBut and a range doesn't work

    anythingBut and a range doesn't work

    Hello,

    It seems it's not possible to do something like:

    $regex->anythingBut('0-9')

    since the sanitize method add systematically a "\" behind the "-" which break the regex range expression. That giving us:

    (?:[^0\-9]*) instead of (?:[^0-9]*)

    Thanks a lot, Aurélien

    opened by AurelienMendes 1
Releases(1.0.0)
Owner
null
A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM.

A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM. s('string')->toTitleCase()->ensureRight('y') ==

Daniel St. Jules 2.5k Dec 28, 2022
"結巴"中文分詞:做最好的 PHP 中文分詞、中文斷詞組件。 / "Jieba" (Chinese for "to stutter") Chinese text segmentation: built to be the best PHP Chinese word segmentation module.

jieba-php "結巴"中文分詞:做最好的 PHP 中文分詞、中文斷詞組件,目前翻譯版本為 jieba-0.33 版本,未來再慢慢往上升級,效能也需要再改善,請有興趣的開發者一起加入開發!若想使用 Python 版本請前往 fxsjy/jieba 現在已經可以支援繁體中文!只要將字典切換為 bi

Fukuball Lin 1.2k Dec 31, 2022
highlight.php is a server-side syntax highlighter written in PHP that currently supports 185 languages

highlight.php is a server-side syntax highlighter written in PHP that currently supports 185 languages. It's a port of highlight.js by Ivan Sagalaev that makes full use of the language and style definitions of the original JavaScript project.

Geert Bergman 633 Dec 27, 2022
Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

Motto: "Every business should have a detection script to detect mobile readers." About Mobile Detect is a lightweight PHP class for detecting mobile d

Şerban Ghiţă 10.2k Jan 4, 2023
A PHP library for generating universally unique identifiers (UUIDs).

ramsey/uuid A PHP library for generating and working with UUIDs. ramsey/uuid is a PHP library for generating and working with universally unique ident

Ben Ramsey 11.9k Jan 8, 2023
👮 A PHP desktop/mobile user agent parser with support for Laravel, based on Mobiledetect

Agent A PHP desktop/mobile user agent parser with support for Laravel, based on Mobile Detect with desktop support and additional functionality. Insta

Jens Segers 4.2k Jan 5, 2023
A lightweight php class for formatting sql statements. Handles automatic indentation and syntax highlighting.

SqlFormatter A lightweight php class for formatting sql statements. It can automatically indent and add line breaks in addition to syntax highlighting

Jeremy Dorn 3.9k Jan 1, 2023
A PHP string manipulation library with multibyte support

A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM. s('string')->toTitleCase()->ensureRight('y') ==

Daniel St. Jules 2.5k Jan 3, 2023
A sane interface for php's built in preg_* functions

Making regex great again Php's built in preg_* functions require some odd patterns like passing variables by reference and treating false or null valu

Spatie 1.1k Jan 4, 2023
A fast PHP slug generator and transliteration library that converts non-ascii characters for use in URLs.

URLify for PHP A fast PHP slug generator and transliteration library, started as a PHP port of URLify.js from the Django project. Handles symbols from

Aband*nthecar 667 Dec 20, 2022
🉑 Portable UTF-8 library - performance optimized (unicode) string functions for php.

?? Portable UTF-8 Description It is written in PHP (PHP 7+) and can work without "mbstring", "iconv" or any other extra encoding php-extension on your

Lars Moelleken 474 Dec 22, 2022
ColorJizz is a PHP library for manipulating and converting colors.

#Getting started: ColorJizz-PHP uses the PSR-0 standards for namespaces, so there should be no trouble using with frameworks like Symfony 2. ###Autolo

Mikeemoo 281 Nov 25, 2022
🔡 Portable ASCII library - performance optimized (ascii) string functions for php.

?? Portable ASCII Description It is written in PHP (PHP 7+) and can work without "mbstring", "iconv" or any other extra encoding php-extension on your

Lars Moelleken 380 Jan 6, 2023
:clamp: HtmlMin: HTML Compressor and Minifier via PHP

??️ HtmlMin: HTML Compressor and Minifier for PHP Description HtmlMin is a fast and very easy to use PHP library that minifies given HTML5 source by r

Lars Moelleken 135 Dec 8, 2022
Generate Heroku-like random names to use in your php applications.

HaikunatorPHP Generate Heroku-like random names to use in your PHP applications. Installation composer require atrox/haikunator Usage Haikunator is p

Atrox 99 Jul 19, 2022
Extensive, portable and performant handling of UTF-8 and grapheme clusters for PHP

Patchwork UTF-8 for PHP Patchwork UTF-8 gives PHP developpers extensive, portable and performant handling of UTF-8 and grapheme clusters. It provides

Nicolas Grekas 80 Sep 28, 2022
PHP library to parse urls from string input

Url highlight - PHP library to parse URLs from string input. Works with complex URLs, edge cases and encoded input. Features: Replace URLs in string b

Volodymyr Stelmakh 77 Sep 16, 2022
A lightweight php class for formatting sql statements. Handles automatic indentation and syntax highlighting.

SqlFormatter A lightweight php class for formatting sql statements. It can automatically indent and add line breaks in addition to syntax highlighting

Jeremy Dorn 3.9k Jan 3, 2023
:accept: Stringy - A PHP string manipulation library with multibyte support, performance optimized

?? Stringy A PHP string manipulation library with multibyte support. Compatible with PHP 7+ 100% compatible with the original "Stringy" library, but t

Lars Moelleken 144 Dec 12, 2022