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
🦉 human-readable regular expressions for PHP

RegExpBuilder integrates regular expressions into the programming language, thereby making them easy to read and maintain. Regular Expressions are created by using chained methods and variables such as arrays or strings.

Max Girkens 907 Dec 30, 2022
Best regular expression for gmail

best regular expression for gmail Gmail Regular expression with all details (not start with dot,number , is it possible to use multiple dot but not in

null 3 Feb 2, 2022
High performance view templating API for PHP applications using tags & expressions inspired by Java JSTL and C compiler

View Language API Table of contents: About Expressions Tags Configuration Compilation Installation Unit Tests Examples Reference Guide About This API

Lucian Gabriel Popescu 0 Jan 9, 2022
Enable method chaining or fluent expressions for any value and method.

PHP Pipe Operator A (hopefully) temporary solution to implement the pipe operator in PHP. Table of contents Requirements How to install How to use The

Sebastiaan Luca 268 Dec 26, 2022
Laravel Integration for Switchover PHP SDK. Feature Toggle Management made easy.

Switchover Laravel Integration Switchover Switchover is a Software-As-A-Service for managing feature toggles (aka switches, flags or feature flips) in

Switchover 6 Nov 6, 2022
Economy made easy for Minecraft: Bedrock

BedrockEconomy Economy made easy for Minecraft Bedrock Commands Name Description Usage Permission balance Show your and others balance balance [player

null 33 Oct 24, 2022
YL MVC Structure (PHP MVC) is a pattern made in PHP used to implement user interfaces, data, and controlling logic.

YL MVC Structure (PHP MVC) is a pattern made in PHP used to implement user interfaces, data, and controlling logic. It is built based on the combination of ideas from the Yii framework and Laravel framework (yl).

Tan Nguyen 3 Jan 3, 2023
The simplest way to create a dynamic sitemap for your self-coded website which you have made by using PHP/HTML/CSS/Js etc... Scripts.

Sitemap_index.xml The simplest way to create a dynamic sitemap for your self-coded website which you have made by using PHP/HTML/CSS/Js etc... Scripts

Tanish Raj 1 Oct 16, 2021
Advanced algorithmic PHP applications I've made

Advanced algorithmic PHP applications I've made

Deniz Uku 1 Nov 2, 2021
Polonium is a world class old school ✍️ blog website made with Php and Tailwind 🌀

Polonium Polonium is a world class old school ✍️ blog website made with Php and Tailwind ?? to write dump articles about... Yeah I know, you should pr

Youness Idbakkasse 2 Jan 10, 2022
Open Source Spotify playlist creator. Made with LOVE on PHP

MotherS_Playlist Description This is an open source application that acts like a playlist manager for Spotify, for now the user can create randomize o

Pedro Caires 2 May 28, 2022
A project of a Login screen made in PHP/CSS3/HTML5/JS with MySQL database integration

A project of a Login screen made in PHP/CSS3/HTML5/JS with MySQL database integration. And animations made with CSS3 and JavaScript itself! ??

Marcel Leite de Farias 2 Apr 26, 2022
All in one Video Downloader - Download videos from facebook twitter youtube tiktok and 1000+ other sites .. made by Vijay Kumar

VKRdownloader Video Downloader by @TherealVKR Vijay Kumar .... Download Video From YouTube , Facebook , Twitter , Instagram , TikTok , And 1000+ Other

Vijay Kumar 35 Dec 29, 2022
✨An Ultimate NPC plugin made by brokiem for PocketMine-MP.

SimpleNPC An Ultimate NPC plugin made by brokiem for PocketMine-MP. ✨ Features Migration from Slapper supported! ✔ Right click to interact! ✔ NPC can

broki 53 Jan 1, 2023
This module aims to validate if the pilot made his flights online on the IVAO and VATSIM networks

SMPirepValidator This module aims to validate if the pilot made his flights online on the IVAO and VATSIM networks SMPirepValidator v.1.0 for phpVMS (

SmartModules for phpVMS 1 Dec 13, 2021
An open-source Laravel 8 online store, client area, and billing software specially made for Pterodactyl panel

PteroBilling An open-source Laravel 8 online store, client area, and billing software specially made for Pterodactyl panel           Announcement: An

PteroBilling 18 Nov 12, 2022
A Lotto plugin made with Pocketmine.

LottoPlugin Features Compatibility with Pocketmine 3.X.X Full Customization Automatic Launch Automatic Draw Usage Initialisation You have to install P

null 3 Nov 20, 2022
A Magento Development Environment Made of Docker Containers

Docker for Magento 1 Extension Development Tl;dr How do I use this? Clone it. Type docker-compose up -d. Install a Magento Mount your file share. Deve

Michael A. Smith 99 May 10, 2021
Phpcs-magento-rules - A set of PHPCS rules used by made.com when hacking Magento

Made.com PHPCS Magento Rules A set of PHPCS rules used by made.com when hacking Magento. Pre-Requisites PHPCS Installation Short Version Clone this re

Made.com Tech Team 26 Jun 3, 2020