You can use the php preg_match_all() function to perform a regular expression (regex) search on a string, and return all matches of the regex pattern found.
$sample_string = "This is a string with some words and 1 number.";
$pattern = '/(? Array
(
[0] => a
[1] => 1
)
)
From the php documentation, the php preg_match_all() function takes 5 parameters:
preg_match_all(
string $pattern,
string $subject,
array &$matches = null,
int $flags = 0,
int $offset = 0
)
The first parameter, $pattern, is the pattern we want to search for. The second parameter, $subject, is the string (or array of strings) we will be searching.
If the third parameter, $matches, is provided, then it is filled with all the matches found during search. $matches[0] will contain the all the matches that matched the full pattern. If you have parenthesized subpatterns, these will be stored in $matches[1], $matches[2], etc.
The fourth parameter, $flags, allows us to receive more information from the preg_match_all() function.
If we want to influence the order of how the matches are returned we can pass ‘PREG_SET_ORDER’ or ‘PREG_PATTERN_ORDER’. ‘PREG_SET_ORDER’ which orders the matches by when the matches are found, and ‘PREG_PATTERN_ORDER’ orders the matches by the full pattern and then the first subpattern, the second subpattern, etc.
Another flag we can pass the ‘PREG_OFFSET_CAPTURE’ flag to receive information about where in the string the first match occurs.
Another flag we can pass is ‘PREG_UNMATCHED_AS_NULL’ which if preg_match_all() doesn’t find a match, the function will return NULL for the unmatched patterns.
Finally, the $offset parameter is where we want to start checking the string. If you want to start checking after the 3rd character, we can pass 3 as the fifth parameter to start checking after the 3rd character.
Using preg_match_all() to Search for a Pattern with php
We can use the php preg_match_all() function to search for a pattern in a string very easily in our php code.
Let’s say I have the following string:
$sample_string = "This is a string with some text that we will search with a regex expression.";
Let’s say I want find all of the four letter words in this string. We can easily do that with the following call to preg_match_all():
$sample_string = "This is a string with some text that we will search and replace with a regex expression.";
$pattern = '/(? Array
(
[0] => This
[1] => with
[2] => some
[3] => text
[4] => that
[5] => will
[6] => with
)
)
The call to preg_match_all() shows there were 7 words that had 4 letters in our string.
Using preg_match_all() to Find All Subpatterns in string
If we want to search for subpatterns in our string, we can do this easily with the preg_match_all() function.
Let’s say we have the following string:
$sample_string = "We have two blue cars, two pink cars, and one blue boat.";
We can use the following pattern to check for all the matches the string has for the following patterns, and preg_match_all() will also return all of the matches for the subpatterns.
$pattern = '((blue|pink) (boat|car))';
We can check our string with the following code:
$sample_string = "We have two blue cars, two pink cars, and one blue boat.";
$pattern = '((blue|pink) (boat|car))';
preg_match_all($pattern,$sample_string,$matches);
print_r($matches);
// Output:
Array
(
[0] => Array
(
[0] => blue car
[1] => pink car
[2] => blue boat
)
[1] => Array
(
[0] => blue
[1] => pink
[2] => blue
)
[2] => Array
(
[0] => car
[1] => car
[2] => boat
)
)
In this case, we see that we have found matches for the full pattern in the first element of the array, and also the subpatterns in the second and third element of the array.
Changing Order of Matches in preg_match_all() with PREG_PATTERN_ORDER Flag
We can manipulate the order the matches are returned in preg_match_all() with the ‘PREG_PATTERN_ORDER’ flag.
The ‘PREG_PATTERN_ORDER’ flag returns the matches in the order of how the patterns are specified. preg_match_all() first returns the full pattern and then the subpatterns from left to right.
In the example below, the first subpattern is if a word is “blue” or “pink”, and the second subpattern is if a word is “boat” or “car”. In this case, we should expect the first subpattern array to be a collection of the matches of the first subpattern (blue|pink), and the second subpattern array to be the matches of the second subpattern (boat|car).
$sample_string = "We have two blue cars, two pink cars, and one blue boat.";
$pattern = '((blue|pink) (boat|car))';
preg_match_all($pattern,$sample_string,$matches,PREG_PATTERN_ORDER);
print_r($matches);
// Output:
Array
(
[0] => Array
(
[0] => blue car
[1] => pink car
[2] => blue boat
)
[1] => Array
(
[0] => blue
[1] => pink
[2] => blue
)
[2] => Array
(
[0] => car
[1] => car
[2] => boat
)
)
Changing Order of Matches in preg_match_all() with PREG_SET_ORDER Flag
We can also manipulate the order the matches are returned in preg_match_all() with the ‘PREG_SET_ORDER’ flag.
The ‘PREG_SET_ORDER’ flag returns the matches in the order of how the matches are found. preg_match_all() returns the first match first and inside the first match array, we receive the full pattern match, and the subpattern matches in the order they occur in the string. Then, the second match comes next and gives us the second full pattern match, and the second subpattern matches.
$sample_string = "We have two blue cars, two pink cars, and one blue boat.";
$pattern = '((blue|pink) (boat|car))';
preg_match_all($pattern,$sample_string,$matches,PREG_SET_ORDER);
print_r($matches);
// Output:
Array
(
[0] => Array
(
[0] => blue car
[1] => blue
[2] => car
)
[1] => Array
(
[0] => pink car
[1] => pink
[2] => car
)
[2] => Array
(
[0] => blue boat
[1] => blue
[2] => boat
)
)
Using preg_match_all() with PREG_OFFSET_CAPTURE Flag
We can find where in our string the matches occurred by adding the PREG_OFFSET_CAPTURE flag in our call to preg_match.
This will add a second element to the $matches object which contains the position of all the matches.
$sample_string = "We have two blue cars, two pink cars, and one blue boat.";
$pattern = '((blue|pink) (boat|car))';
preg_match_all($pattern,$sample_string,$matches,PREG_OFFSET_CAPTURE);
print_r($matches);
// Output:
Array
(
[0] => Array
(
[0] => Array
(
[0] => blue car
[1] => 12
)
[1] => Array
(
[0] => pink car
[1] => 27
)
[2] => Array
(
[0] => blue boat
[1] => 46
)
)
[1] => Array
(
[0] => Array
(
[0] => blue
[1] => 12
)
[1] => Array
(
[0] => pink
[1] => 27
)
[2] => Array
(
[0] => blue
[1] => 46
)
)
[2] => Array
(
[0] => Array
(
[0] => car
[1] => 17
)
[1] => Array
(
[0] => car
[1] => 32
)
[2] => Array
(
[0] => boat
[1] => 51
)
)
)
Using preg_match_all() with PREG_UNMATCHED_AS_NULL Flag
The default behavior of preg_match_all() is to return unmatched subpatterns as an empty string or “”.
You can pass the PREG_UNMATCHED_AS_NULL flag to instead return NULL for those subpatterns if the function doesn’t find a match.
$sample_string = "We have two blue cars, two green apples, and one blue boat.";
$pattern = '((blue|pink)*(boat|car))';
preg_match_all($pattern,$sample_string,$matches,PREG_UNMATCHED_AS_NULL);
var_dump($matches);
// Output:
array(3) {
[0]=>
array(2) {
[0]=>
string(3) "car"
[1]=>
string(4) "boat"
}
[1]=>
array(2) {
[0]=>
NULL
[1]=>
NULL
}
[2]=>
array(2) {
[0]=>
string(3) "car"
[1]=>
string(4) "boat"
}
}
Using preg_match_all() with Offset
The fifth parameter is the offset or where we want to start our search in the string.
Let’s say I have the string from the first example and I want to see if the word “This” is in the string. However, this time I want to start from the 10th character in the string. To check if the word “This” is in the string, we use the code below and unsurprisingly, preg_match_all() does not find a match:
$sample_string = "This is a string with some text that we will search with a regex expression.";
$pattern = '/This/';
preg_match_all($pattern,$sample_string,$matches,PREG_OFFSET_CAPTURE,10);
print_r($matches);
// Output:
Array
(
)
Hopefully this article has been helpful for you to understand how you can use the php preg_match_all() function to find all matches of a pattern in strings based on regex expressions in your php code.