codekandis/regular-expressions
is a library to provide regular expression interfaces and classes.
Install the latest version with
$ composer require codekandis/regular-expressions
RegularExpression
wraps several PHP RegEx functions. The description to the paramters used can be read in the PHP manual linked in the description of the respective methods. The parameter [$throwNoMatchException
][] is described below
The pattern must be passed to the constructor of the RegularExpression
.
$regularExpression = new RegularExpression( '~[a-z]+[0-9]+~' );
If the passed pattern is invalid an InvalidRegularExpressionException
will be thrown.
RegularExpression::match()
wraps PHP's function preg_match()
.
$regularExpression = new RegularExpression( '~[a-z]+[0-9]+~' );
$match = $regularExpression->match( 'foo01234_bar56789' );
/**
* $match = [
* 'foo01234'
* ]
*/
This method provides a parameter $throwNoMatchException
. It defaults to true
. If the regular expression does not match to the subject a RegularExpressionNotMatchingException
will be thrown. If the parameter $throwNoMatchException
is set to false
the method returns null
.
Passing an invalid offset throws an InvalidOffsetException
.
RegularExpression::matchAll()
wraps PHP's function preg_match_all()
.
$regularExpression = new RegularExpression( '~[a-z]+[0-9]+~' );
$matches = $regularExpression->matchAll( 'foo01234_bar56789' );
/**
* $matches = [
* [
* 'foo01234',
* 'bar56789'
* ]
* ]
*/
This method provides a parameter $throwNoMatchException
. It defaults to true
. If the regular expression does not match to the subject a RegularExpressionNotMatchingException
will be thrown. If the parameter $throwNoMatchException
is set to false
the method returns null
.
Passing an invalid offset throws an InvalidOffsetException
.
RegularExpression::replace()
wraps PHP's function preg_replace()
.
$regularExpression = new RegularExpression( '~[a-z]+[0-9]+~' );
$replacedSubject = $regularExpression->replace( 'foo01234_bar56789', 'replacement' );
/**
* $replacedSubject = 'replacement_replacement'
*/
This method provides a parameter $throwNoMatchException
. It defaults to true
. If the regular expression does not match to the subject a RegularExpressionNotMatchingException
will be thrown. If the parameter $throwNoMatchException
is set to false
the method returns the passed subject instead.
Passing an invalid limit throws an InvalidLimitException
.