Php's built in preg_*
functions require some odd patterns like passing variables by reference and treating false
or null
values as errors. spatie/regex
provides a cleaner interface for preg_match
, preg_match_all
, preg_replace
and preg_replace_callback
.
use Spatie\Regex\Regex;
// Using `match`
Regex::match('/a/', 'abc'); // `MatchResult` object
Regex::match('/a/', 'abc')->hasMatch(); // true
Regex::match('/a/', 'abc')->result(); // 'a'
// Capturing groups with `match`
Regex::match('/a(b)/', 'abc')->result(); // 'ab'
Regex::match('/a(b)/', 'abc')->group(1); // 'a'
// Using `matchAll`
Regex::matchAll('/a/', 'abcabc')->hasMatch(); // true
Regex::matchAll('/a/', 'abcabc')->results(); // Array of `MatchResult` objects
// Using replace
Regex::replace('/a/', 'b', 'abc')->result(); // 'bbc';
Regex::replace('/a/', function (MatchResult $result) {
return $result->result() . 'Hello!';
}, 'abc')->result(); // 'aHello!bc';
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.
The best postcards will get published on the open source page on our website.
You can install the package via composer:
composer require spatie/regex
Matches a pattern on a subject. Returns a MatchResult
object for the first match.
/**
* @param string $pattern
* @param string $subject
*
* @return \Spatie\Regex\MatchResult
*/
Regex::match(string $pattern, string $subject): MatchResult
Checks if the pattern matches the subject.
Regex::match('/abc/', 'abc')->hasMatch(); // true
Regex::match('/def/', 'abc')->hasMatch(); // false
Return the full match that was made. Returns null
if no match was made.
Regex::match('/abc/', 'abc')->result(); // 'abc'
Regex::match('/def/', 'abc')->result(); // null
Return the contents of a captured group (with a 1-based index). Throws a RegexFailed
exception if the group doesn't exist.
Regex::match('/a(b)c/', 'abc')->group(1); // 'b'
Regex::match('/a(b)c/', 'abc')->group(2); // `RegexFailed` exception
Matches a pattern on a subject. Returns a MatchAllResult
object containing all matches.
/**
* @param string $pattern
* @param string $subject
*
* @return \Spatie\Regex\MatchAllResult
*/
public static function matchAll(string $pattern, string $subject): MatchAllResult
Checks if the pattern matches the subject.
Regex::matchAll('/abc/', 'abc')->hasMatch(); // true
Regex::matchAll('/abc/', 'abcabc')->hasMatch(); // true
Regex::matchAll('/def/', 'abc')->hasMatch(); // false
Returns an array of MatchResult
objects.
$results = Regex::matchAll('/ab([a-z])/', 'abcabd')->results();
$results[0]->result(); // 'abc'
$results[0]->group(1); // 'c'
$results[1]->result(); // 'abd'
$results[1]->group(1); // 'd'
Replaces a pattern in a subject. Returns a ReplaceResult
object.
/**
* @param string|array $pattern
* @param string|array|callable $replacement
* @param string|array $subject
* @param int $limit
*
* @return \Spatie\Regex\ReplaceResult
*/
public static function replace($pattern, $replacement, $subject, $limit = -1): ReplaceResult
Regex::replace('/a/', 'b', 'abc')->result(); // 'bbc'
Regex::replace
also works with callables. The callable will receive a MatchResult
instance as it's argument.
Regex::replace('/a/', function (MatchResult $matchResult) {
return str_repeat($matchResult->result(), 2);
}, 'abc')->result(); // 'aabc'
Patterns, replacements and subjects can also be arrays. Regex::replace
behaves exactly like preg_replace
in those instances.
If anything goes wrong in a Regex
method, a RegexFailed
exception gets thrown. No need for checking preg_last_error()
.
Please see CHANGELOG for more information what has changed recently.
$ composer test
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
The MIT License (MIT). Please see License File for more information.