PHP Regular Expressions (Perl-Compatible)
<?php
preg_match('/(foo)(bar)(baz)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
Output
Array
(
[0] => Array
(
[0] => foobarbaz
[1] => 0
)
[1] => Array
(
[0] => foo
[1] => 0
)
[2] => Array
(
[0] => bar
[1] => 3
)
[3] => Array
(
[0] => baz
[1] => 6
)
)
Requirements
No external libraries are needed to build this extension.
Installation
The PCRE extension is a core PHP extension, so it is always enabled.
By default, this extension is compiled using the bundled PCRE library.
Alternatively, an external PCRE library can be used by passing in the --with-pcre-regex=DIR configuration option where DIR is the location of PCRE's include and library files.
It is recommended to use PCRE 8.10 or newer for PHP 5.6 and 7.0.
As of PHP 7.0.0 PCRE's just-in-time compilation is supported by default, which can be disabled with the --without-pcre-jit configuration option as of PHP 7.0.12.
The Windows version of PHP has built-in support for this extension. You do not need to load any additional extensions in order to use these functions.
Run-time Configuration
The behaviour of these functions is affected by settings in php.ini.
PCRE Configuration Options
Name | Default | Changeable | Changelog |
---|---|---|---|
pcre.backtrack_limit | "1000000" | PHP_INI_ALL | Available since PHP 5.2.0. |
pcre.recursion_limit | "100000" | PHP_INI_ALL | Available since PHP 5.2.0. |
pcre.jit | "1" | PHP_INI_ALL | Available since PHP 7.0.0. |
Resource Types
This extension has no resource types defined.
Predefined Constants
This Regular expression extension also provided some predefined constants.
In order to use this extension has either been compiled into PHP or dynamically loaded at runtime.
PREG constants
-
PREG_PATTERN_ORDER
-
Orders result so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on.
-
This flag is only used with preg_match_all().
-
-
PREG_SET_ORDER
-
Orders result so that $matches[0] is an array of the first set of matches, $matches[1] is an array of the second set of matches, and so on.
-
This flag is only used with preg_match_all().
-
-
PREG_OFFSET_CAPTURE
-
See the description of PREG_SPLIT_OFFSET_CAPTURE. 4.3.0
-
-
PREG_SPLIT_NO_EMPTY
-
This flag tells preg_split() to return only non-empty pieces.
-
-
PREG_SPLIT_DELIM_CAPTURE
-
This flag tells preg_split() to capture parenthesized expression in the delimiter pattern as well. 4.0.5
-
-
PREG_SPLIT_OFFSET_CAPTURE
-
If this flag is set, for every occurring match the appendant string offset will also be returned.
-
Note that this changes the return values in an array where every element is an array consisting of the matched string at offset 0 and its string offset within-subject at offset 1.
-
This flag is only used for preg_split(). 4.3.0
-
-
PREG_UNMATCHED_AS_NULL
-
This flag tells preg_match() and preg_match_all() to include unmatched subpatterns in $matches as NULL values.
-
Without this flag, unmatched subpatterns are reported as empty strings, as if they were empty matches.
-
Setting this flag allows distinguishing between these two cases. 7.2.0
-
-
PREG_NO_ERROR
-
Returned by preg_last_error() if there were no errors. 5.2.0
-
-
PREG_INTERNAL_ERROR
-
Returned by preg_last_error() if there was an internal PCRE error. 5.2.0
-
-
PREG_BACKTRACK_LIMIT_ERROR
-
Returned by preg_last_error() if backtrack limit was exhausted. 5.2.0
-
-
PREG_RECURSION_LIMIT_ERROR
-
Returned by preg_last_error() if recursion limit was exhausted. 5.2.0
-
-
PREG_BAD_UTF8_ERROR
-
Returned by preg_last_error() if the last error was caused by malformed UTF-8 data (only when running a regex in UTF-8 mode). 5.2.0
-
-
PREG_BAD_UTF8_OFFSET_ERROR
-
Returned by preg_last_error() if the offset didn't correspond to the beginning of a valid UTF-8 code point (only when running a regex in UTF-8 mode). 5.3.0
-
-
PREG_JIT_STACKLIMIT_ERROR
-
Returned by preg_last_error() if the last PCRE function failed due to limited JIT stack space. 7.0.0
-
-
PCRE_VERSION
-
PCRE version and release date (e.g. "7.0 18-Dec-2006"). 5.2.4
-
PCRE Functions
-
preg_filter — Perform a regular expression search and replace
-
preg_grep — Return array entries that match the pattern
-
preg_last_error — Returns the error code of the last PCRE regex execution
-
preg_match_all — Perform a global regular expression match
-
preg_match — Perform a regular expression match
-
preg_quote — Quote regular expression characters
-
preg_replace_callback_array — Perform a regular expression search and replace using callbacks
-
preg_replace_callback — Perform a regular expression search and replace using a callback
-
preg_replace — Perform a regular expression search and replace
-
preg_split — Split string by a regular expression