preg_match

FunctionParams and return types changed in PHP 8.0

Perform a regular expression match.

PHP 5
PHP 7
PHP 8.0
Improved
PHP 8.1
PHP 8.2-8.3
PHP 8.4
PHP 8.5
PHP 8.6

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 $matches is 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: $subpatterns to $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

PHP VersionAvailability
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