preg_match
Description
The preg_match of PCRE for PHP perform a regular expression match.
Syntax
preg_match( string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0 ): int|false
Parameters
pattern
The pattern to search for, as a string.
subject
The input string.
matches
If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
flags
flags can be a combination of the following flags:
Contant | Description |
---|---|
PREG_OFFSET_CAPTURE | For every occurring match the appendant string offset (in bytes) will also be returned. Note that this changes the value of matches into an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1. |
PREG_UNMATCHED_AS_NULL | Unmatched subpatterns are reported as NULL; otherwise they are reported as an empty string. |
offset
Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search (in bytes).
NOTE: Using offset is not equivalent to passing substr($subject, $offset) to preg_match() in place of the subject string, because pattern can contain assertions such as ^, $ or (?<=x). Alternatively, to avoid using substr(), use the \G assertion rather than the ^ anchor, or the A modifier instead, both of which work with the offset parameter.
Return
Returns 1 if the pattern matches given subject, 0 if it does not, or false on failure.
WARNING: This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
Examples
1 · pattern subject · case-sensitive
<? $pattern = "#php#"; $subject = "PHP stands for PHP: Hypertext Preprocessor"; $return = preg_match($pattern, $subject); echo $return; ?>
0
2 · pattern subject · case-insensitive
<? $pattern = "#php#i"; $subject = "PHP stands for PHP: Hypertext Preprocessor"; $return = preg_match($pattern, $subject); echo $return; ?>
1
3 · matches
<? $pattern = "#php#i"; $subject = "PHP stands for PHP: Hypertext Preprocessor"; preg_match($pattern, $subject, $matches); print_r($matches); ?>
Array ( [0] => PHP )
4 · flags · PREG_OFFSET_CAPTURE
<? $pattern = "#(preg)(offset)(capture)#"; $subject = "pregoffsetcapture"; $flags = PREG_OFFSET_CAPTURE; preg_match($pattern, $subject, $matches, $flags); print_r($matches); ?>
Array ( [0] => Array ( [0] => pregoffsetcapture [1] => 0 ) [1] => Array ( [0] => preg [1] => 0 ) [2] => Array ( [0] => offset [1] => 4 ) [3] => Array ( [0] => capture [1] => 10 ) )
5 · flags · PREG_UNMATCHED_AS_NULL
<? $pattern = "#(preg)(unmatched)*(as)#"; $subject = "pregas"; $flags = PREG_UNMATCHED_AS_NULL; preg_match($pattern, $subject, $matches1); preg_match($pattern, $subject, $matches2, $flags); var_export($matches1); echo PHP_EOL; var_export($matches2); ?>
array ( 0 => 'pregas', 1 => 'preg', 2 => '', 3 => 'as', ) array ( 0 => 'pregas', 1 => 'preg', 2 => NULL, 3 => 'as', )
6 · offset
<? $pattern = "#offset#"; $subject = "offset offset offset"; $flags = PREG_OFFSET_CAPTURE; $offset = 1; preg_match($pattern, $subject, $matches, $flags, $offset); print_r($matches); ?>
Array ( [0] => Array ( [0] => offset [1] => 7 ) )
7 · word boundary
<? $pattern = "#\bweb\b#"; $subject1 = "PHP is the web scripting language of choice."; $subject2 = "PHP is the website scripting language of choice."; $return1 = preg_match($pattern, $subject1); $return2 = preg_match($pattern, $subject2); echo $return1 . PHP_EOL; echo $return2; ?>
1 0
8 · named subpattern
<? $pattern = "#(?<name>\w+): (?<digit>\d+)#"; $subject = "year: 2001"; preg_match($pattern, $subject, $matches); print_r($matches); ?>
Array ( [0] => year: 2001 [name] => year [1] => year [digit] => 2001 [2] => 2001 )
9 · URL
<? $pattern = "#^(?:https?://)?([^/]+)#i"; $subject = "https://sub.domain.com/"; preg_match($pattern, $subject, $matches); print_r($matches); $pattern = "#[^.]+\.[^.]+$#"; $subject = $matches[1]; preg_match($pattern, $subject, $matches); print_r($matches); ?>
Array ( [0] => https://sub.domain.com [1] => sub.domain.com ) Array ( [0] => domain.com )