You can use the php preg_match() function to perform a regular expression (regex) search on a string, and return if a match of the regex pattern found, and find out what the first match is.
$sample_string = "This is a string with some words.";
$pattern = '/(?
preg_match() only returns information about the first match. If you'd like to get information on all of the matches, you should use the php preg_match_all() function.
From the php documentation, the php preg_match() function takes 5 parameters:
preg_match(
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 the first match found during search. $matches[0] will contain the first match 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() function. For example, 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() 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() to Search for a Pattern with php
We can use the php preg_match() 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 if there are any five letter words in this string. We can easily do that with the following call to preg_match():
$sample_string = "This is a string with some text that we will search and replace with a regex expression.";
$pattern = '/(? regex
)
Using preg_match() to Find Subpatterns in string
If we want to search for subpatterns in our string, we can do this easily with preg_match.
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 if the string has "blue car" but also if it has the subpatterns as well.
$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($pattern,$sample_string,$matches);
print_r($matches);
// Output:
Array
(
[0] => blue car
[1] => blue
[2] => car
)
In this case, we see that the we found matches for the full pattern and also the subpatterns. Again, here, preg_match() is only bringing the first match. If we used preg_match_all(), we would have received matches for pink car as well.
Using preg_match() with PREG_OFFSET_CAPTURE Flag
We can find where in our string the match first occurs 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 the first match.
$sample_string = "We have two blue cars, two green apples, and one blue boat.";
$pattern = '((blue|pink) (boat|car))';
preg_match($pattern,$sample_string,$matches,PREG_OFFSET_CAPTURE);
print_r($matches);
// Output:
Array
(
[0] => Array
(
[0] => blue car
[1] => 12
)
[1] => Array
(
[0] => blue
[1] => 12
)
[2] => Array
(
[0] => car
[1] => 17
)
)
We can see that the full pattern was first matched at position 12, the first subpattern was matched at position 12, and the second subpattern was first matched at 17.
Using preg_match() with PREG_UNMATCHED_AS_NULL Flag
The default behavior of preg_match() 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($pattern,$sample_string,$matches,PREG_UNMATCHED_AS_NULL );
var_dump($matches);
// Output:
Array
array(3) {
[0]=>
string(3) "car"
[1]=>
NULL
[2]=>
string(3) "car"
}
Using preg_match() 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() does not find a match:
$sample_string = "This is a string with some text that we will search with a regex expression.";
$pattern = '/This/';
echo preg_match($pattern,$sample_string,$matches,PREG_OFFSET_CAPTURE,10); // Output: 0
print_r($matches);
// Output:
Array
(
)
Hopefully this article has been helpful for you to understand how you can use the php preg_match() function to find patterns in strings based on regex expressions in your php code.