preg_match
Perform a regular expression match.
preg_match Function synopsis
preg_match(
string $pattern,
string $subject,
&$matches = null,
int $flags = 0,
int $offset = 0
): int|false
Parameters
$pattern
Typestring
The pattern to search for, as a string.
$subject
Typestring
The input string.
$matches
Passed by reference, OptionalDefault valuenull
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
OptionalTypeintDefault value0
$flags can be a combination of the following flags:
$flags accepts one or a bitmask of the following constant values:
$offset
OptionalTypeintDefault value0
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 that the parameter
$matchesis passed by reference, and contains additional/updated context data that may be useful.
Return value
Typeint|false
preg_match returns 1 if the $pattern
matches given $subject, 0 if it does not, or false on failure.
Changes to the preg_match Function
PHP 8.0
- Return type added:
int|false - Parameter type added for parameter #1 (
$pattern):string - Parameter type added for parameter #2 (
$subject):string - Parameter name of parameter #3 changed:
$subpatternsto$matches - Parameter default value added for position #3 (
$matches):null - Parameter type added for parameter #4 (
$flags):int - Parameter default value added for position #4 (
$flags):0 - Parameter type added for parameter #5 (
$offset):int - Parameter default value added for position #5 (
$offset):0
preg_match(
- $pattern,
+ string $pattern,
- $subject,
+ string $subject,
- &$subpatterns,
+ &$matches = null,
- $flags,
+ int $flags = 0,
- $offset
+ int $offset = 0
- )
+ ): int|false preg_match Function Availability
preg_match Function Availability| PHP Version | Availability |
|---|---|
| PHP 8.6Upcoming Release | Yes |
| PHP 8.5Supported (Latest) | Yes |
| PHP 8.4Supported | Yes |
| PHP 8.3Security-Fixes Only | Yes |
| PHP 8.2Security-Fixes Only | Yes |
| PHP 8.1Unsupported | Yes |
| PHP 8.0Unsupported | Yes |
| PHP 7.4Unsupported | Yes |
| PHP 7.3Unsupported | Yes |
| PHP 7.2Unsupported | Yes |
| PHP 7.1Unsupported | Yes |
| PHP 7.0Unsupported | Yes |
| PHP 5.6Unsupported | Yes |
| PHP 5.5Unsupported | Yes |
| PHP 5.4Unsupported | Yes |
| PHP 5.3Unsupported | Yes |