Download the PHP package regex-guard/regex-guard without Composer
On this page you can find all versions of the php package regex-guard/regex-guard. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package regex-guard
RegexGuard
Why?
PHP core preg_*
functions do not offer any good way to validate a regular expression before usage. Some core functions return false for invalid regular expressions but they also emit uncatchable warnings.
RegexGuard is a wrapper that allows you to validate regular expressions and keep your API away from uncatchable PCRE compilation warnings.
Composer Installation
Through terminal: composer require regex-guard/regex-guard:~1.0
:8ball:
Quick Example
First grab a RegexGuard instance:
Validate your regex:
And there is more...
RegexGuard API
Internally, RegexGuard instance sandboxes all preg_*
functions calls and handle errors in a convenient way.
All preg_*
core functions are fully represented:
::isRegexValid($pattern)
Validates a given regexp. Returns true when PCRE string is valid, false otherwise:
::validateRegexOrFail($pattern)
Validates a given regexp or throw \RegexGuard\RegexException
if PCRE is invalid:
::match($pattern, $subject, &$matches=null, $flags=0, $offset=0)
Same as preg_match but throws a \RegexGuard\RegexException
when an invalid PCRE string is given:
::matchAll($pattern,$subject,&$matches=null,$flags=?,$offset=0)
Same as preg_match_all but throws a \RegexGuard\RegexException
when an invalid PCRE string is given:
NOTE:
$flags
default value depends on your PHP version.
::filter($pattern, $subject, $limit = -1, $flags = 0)
Same as preg_filter but throws a \RegexGuard\RegexException
when an invalid PCRE string is given:
::grep($pattern, $input, $flags = 0)
Same as preg_grep but throws a RegexGuard\RegexException
when an invalid PCRE string is given:
::replace($pattern, $replace, $subject, $limit=-1, &$count=null)
Same as preg_replace but throws a \RegexGuard\RegexException
when an invalid PCRE string is given:
::split($pattern, $subject, $limit = -1, $flags = 0)
Same as preg_split but throws a \RegexGuard\RegexException
when an invalid PCRE string is given:
Avoiding Exceptions
You can avoid try catch
blocks by telling RegexGuard not to throw exceptions when an invalid regular expression is encountered:
This can be useful to avoid verbosity when your API needs to validate regexp arguments all over the place, but you will have to be extra careful when checking results!
Manual Instantiation
Features
- No need for
@preg_match
,@preg_match_all
... - No need to use regexp to validate a given regexp or any other crappy solution
- Aims to be 100% compatible with PHP
preg_*
core functions - Faster and more reliable than
@
+preg_*
calls - Compatible with xdebug.scream
Contribution Guide
- Fork regex-guard
- Clone forked repository
- Install composer dependencies
$ composer install
- Run unit tests
$ phpunit
- Modify code: correct bug, implement feature
- Back to step 4
- Pull request to master branch
Copyright
Copyright (c) 2014 Márcio Almada. Distributed under the terms of an MIT-style license. See LICENSE for details.