PHP Function Reference

PHP - Regular Expression

The PCRE - Regular Expressions (Perl-Compatible) library is a set of functions that allow you to implement regular expression pattern matching. The syntax for patterns used in these functions closely resembles Perl.


There is no installation needed to use these functions. These functions are part of the PHP core.

Runtime Configuration

The behavior of these functions is affected by settings in php.ini.

PCRE Configuration Options

pcre.backtrack_limit"1000000"PCRE's backtracking limit. Defaults to 100000 for PHP < 5.3.7.PHP_INI_ALL
pcre.recursion_limit"100000"PCRE's recursion limit. Please note that if set to a high number it may consume all the available process stack and eventually crash PHP (due to reaching the stack size limit imposed by the Operating System).PHP_INI_ALL
pcre.jit"1"When set to "1" enables PCRE's just-in-time compilation.PHP_INI_ALL

PHP Regular Expression Functions

preg_filter() Perform a regular expression search and replace.
preg_grep() Return array entries that match the pattern.
preg_last_error_msg() Returns the error message of the last PCRE regex execution.
preg_last_error() Returns the error code of the last PCRE regex execution.
preg_quote() Quote regular expression characters.
preg_replace() Perform a regular expression search and replace.
preg_split() Split string by a regular expression.

PHP Regular Expression Predefined Constants

The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.

ConstantsDescriptionAs of
PREG_PATTERN_ORDEROrders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized sub-pattern, and so on. This flag is only used with preg_match_all().
PREG_SET_ORDEROrders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on. This flag is only used with preg_match_all().
PREG_SPLIT_NO_EMPTYThis flag tells preg_split() to return only non-empty pieces.
PREG_SPLIT_DELIM_CAPTUREThis flag tells preg_split() to capture parenthesized expression in the delimiter pattern as well.
PREG_SPLIT_OFFSET_CAPTUREIf 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().
PREG_UNMATCHED_AS_NULLThis flag tells preg_match() and preg_match_all() to include unmatched sub-patterns in $matches as null values. Without this flag, unmatched sub-patterns are reported as empty strings, as if they were empty matches. Setting this flag allows to distinguish between these two cases.7.2.0
PREG_NO_ERRORReturned by preg_last_error() if there were no errors.5.2.0
PREG_INTERNAL_ERRORReturned by preg_last_error() if there was an internal PCRE error.5.2.0
PREG_BACKTRACK_LIMIT_ERRORReturned by preg_last_error() if backtrack limit was exhausted.5.2.0
PREG_RECURSION_LIMIT_ERRORReturned by preg_last_error() if recursion limit was exhausted.5.2.0
PREG_BAD_UTF8_ERRORReturned 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_ERRORReturned by preg_last_error() if the offset didn't correspond to the begin of a valid UTF-8 code point (only when running a regex in UTF-8 mode).5.3.0
PREG_JIT_STACKLIMIT_ERRORReturned by preg_last_error() if the last PCRE function failed due to limited JIT stack space.7.0.0
PCRE_VERSIONPCRE version and release date (e.g. "7.0 18-Dec-2006").5.2.4