preg_match_all

FunctionParams and return types changed in PHP 8.0

Perform a global 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_all Function synopsis

preg_match_all(
    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

Array of all matches in multi-dimensional array ordered according to $flags.

$flags

OptionalTypeintDefault value0

Can be a combination of the following flags (note that it doesn't make sense to use PREG_PATTERN_ORDER together with PREG_SET_ORDER):

$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

Returns the number of full pattern matches (which might be zero), or false on failure.

Changes to the preg_match_all 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_all(
-     $pattern,
+     string $pattern,
-     $subject,
+     string $subject,
-     &$subpatterns,
+     &$matches = null,
-     $flags,
+     int $flags = 0,
-     $offset
+     int $offset = 0
-   )
+   ): int|false

preg_match_all 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