Easy Tutorial
❮ Php Var_Dump Function Func Date Timezone Name Get ❯

PHP Regular Expressions (PCRE)

Regular expressions (regex) describe a pattern for matching characters in strings. They can be used to check if a string contains a certain substring, replace matching substrings, or extract substrings that meet certain criteria from a string.

For more details on regular expressions, please refer to our: Regular Expressions - Tutorial.

In PHP, we can use the PCRE extension to match string patterns.

PCRE Functions

Function Description
preg_filter Performs a regular expression search and replace
preg_grep Returns array entries that match the pattern
preg_last_error Returns the error code of the last PCRE regex execution
preg_match_all Performs a global regular expression match
preg_match Performs a regular expression match
preg_quote Escapes regex characters
preg_replace_callback_array Performs a regex search and replace using callbacks
preg_replace_callback Performs a regex search and replace using a callback
preg_replace Performs a regex search and replace
preg_split Splits a string by a regex

PREG Constants

Constant Description Since Version
PREG_PATTERN_ORDER Results are sorted by "pattern", only for preg_match_all(), where $matches[0] is the full pattern match, $matches[1] is the result of the first subpattern, etc.
PREG_SET_ORDER Results are sorted by "set", only for preg_match_all(), where $matches[0] contains all results of the first match (including subpatterns), $matches[1] contains all results of the second match, etc.
PREG_OFFSET_CAPTURE See description for PREG_SPLIT_OFFSET_CAPTURE. 4.3.0
PREG_SPLIT_NO_EMPTY This flag tells preg_split() to return only non-empty parts.
PREG_SPLIT_DELIM_CAPTURE This flag tells preg_split() to also capture contents of parenthesized expressions. 4.0.5
PREG_SPLIT_OFFSET_CAPTURE If this flag is set, the offset of each match is also returned. This changes the return values, with each element being an array where the 0th element is the matched string and the 1st element is its offset in the target string. This flag is only for preg_split(). 4.3.0
PREG_NO_ERROR Returned by preg_last_error() when there is no error. 5.2.0
PREG_INTERNAL_ERROR Returned by preg_last_error() when there is an internal PCRE error. 5.2.0
PREG_BACKTRACK_LIMIT_ERROR Returned by preg_last_error() when the backtrack limit is exceeded. 5.2.0
PREG_RECURSION_LIMIT_ERROR Returned by preg_last_error() when the recursion limit is exceeded. 5.2.0
PREG_BAD_UTF8_ERROR Returned by preg_last_error() when the last error is due to malformed UTF-8 data (only when running in UTF-8 mode). 5.2.0
PREG_BAD_UTF8_OFFSET_ERROR If the offset does not match a valid UTF-8 code (only available in UTF-8 mode regular expressions). Returned by preg_last_error(). 5.3.0
PCRE_VERSION PCRE version number and release date (e.g., "7.0 18-Dec-2006"). 5.2.4
❮ Php Var_Dump Function Func Date Timezone Name Get ❯