• This repository has been archived on 23/Jan/2021
  • Stars
    star
    220
  • Rank 179,868 (Top 4 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 11 years ago
  • Updated about 11 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easy regular expression building

VerbalExpressionsPhp

PHP port of jehna/VerbalExpressions.

Installation

Composer

Add to composer.json:-

{
    "require": {
        ...,
        "markwilson/verbal-expressions-php": "dev-master"
    }
}

Example usage

<?php

require_once 'vendor/autoload.php';

use MarkWilson\VerbalExpression;
use MarkWilson\VerbalExpression\Matcher;

// initialise verbal expression instance
$verbalExpression = new VerbalExpression();

// URL matcher
$verbalExpression->startOfLine()
                 ->then('http')
                 ->maybe('s')
                 ->then('://')
                 ->maybe('www.')
                 ->anythingBut(' ')
                 ->endOfLine();

// compile expression - returns ^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$
$verbalExpression->compile();

// perform match
preg_match($verbalExpression, 'http://www.google.com'); // returns 1
// or
$matcher = new Matcher();
$matcher->isMatch($verbalExpression, 'http://www.google.com'); // returns true

Nesting expressions

<?php

$innerExpression = new VerbalExpression();
$innerExpression->word();

$outerExpression = new VerbalExpression();
$outerExpression->startOfLine()
                ->find($innerExpression)
                ->then($innerExpression)
                ->endOfLine();

// returns ^(\w+)(\w+)$
$outerExpression->compile();

Disable sub pattern capturing

By default, sub patterns are captured and will be returned in the matches array.

<?php

// disable sub pattern capture
$verbalExpression->disableSubPatternCapture()->word(); // (?:\w+)
// or
$verbalExpression->word(false); // (?:\w+)

Disabling this will only affect subsequent additions to the expression; any already added will be unaffected. This allows for disabling and enabling in groups.

<?php

// equivalent to (\w+)(?:\w+)(?:\w+)(\w+)
$verbalExpression->word()
                 ->disableSubPatternCapture()
                 ->word()
                 ->word()
                 ->enableSubPatternCapture()
                 ->word();

More Repositories

1

AwsCloudSearchPhp

A library to integrate with basic AWS CloudSearch API
PHP
11
star
2

html2pdf

GitHub action to convert HTML to PDF
Dockerfile
7
star
3

symfony2-validator-priority-chain

Prioritised chain of validator constraints in Symfony2 validator component
PHP
5
star
4

PushState

Very basic example of pushState for template content reload w/ PHP
JavaScript
3
star
5

IMDb-actor-connections

Import and search IMDb for connections between actors
PHP
3
star
6

publictweetstream-node

Stream tweets from Twitter
JavaScript
2
star
7

ThreeLeggedOAuth

Base OAuth 3-legged helper class
PHP
2
star
8

jquery.shortentext.js

Shorten text to fit in a specified width
JavaScript
2
star
9

array-flatten

Adds array_flatten(array) global function
PHP
2
star
10

symfony2-validator-checksum

Symfony2 validator checksum constraint
PHP
2
star
11

array-key-filter

PHP array filtering by key
PHP
2
star
12

static-server

Simple static HTTP server
Go
1
star
13

UrlShortener

A simple PHP URL shortening service
PHP
1
star
14

array-permutations

PHP
1
star
15

wp-composer-plugin

Wordpress Composer Plugin to be used with markwilson/wp-template
PHP
1
star
16

array-filtering

PHP array filtering
PHP
1
star
17

Errors.js

Basic JavaScript error handling
JavaScript
1
star
18

proxyssh.sh

Bash script to simplify connecting to a remote SSH server via a proxy SSH server
Shell
1
star
19

symfony2-validator-native-filter

Symfony2 validator native PHP filter constraint
PHP
1
star
20

DateRange

Date ranges
PHP
1
star
21

symfony2-validator-fields-intersect-not-empty

Constraint to check at least one of the keys exist in an array
PHP
1
star
22

publictweetstream-php

Basic streaming API connection in PHP using React
PHP
1
star
23

jQuery-IntervalX

Run intervals a specific number of times
JavaScript
1
star