English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP Regular Expression (PCRE)

A regular expression (regular expression) describes a pattern of string matching that can be used to check if a string contains a certain substring, replace the matched substring, or extract a substring that meets certain conditions from a string, etc.

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

Regular expression function

FunctionDescription
preg_filterPerform a search and replace with a regular expression
preg_grepReturn the array entry of the matched pattern
preg_last_errorReturn the last error code generated by the PCRE regular expression execution
preg_match_allPerform a global regular expression match
preg_matchPerform a regular expression match
preg_quoteEscape regular expression characters
preg_replace_callback_arrayPerform a regular expression search and replace using a callback
preg_replace_callbackPerform a regular expression search and replace using a callback
preg_replacePerform a search and replace with a regular expression
preg_splitSplit a string by a regular expression

PREG Constants

ConstantDescriptionFrom which version
PREG_PATTERN_ORDERresults are sorted by "rule", only for preg_match_all(), i.e., $matches[0] is the complete rule matching result, $matches[1] is the result of the first subgroup match, etc.since
PREG_SET_ORDERresults are sorted by "collection", only for preg_match_all(), i.e., $matches[0] saves all the result information of the first matching results (including subgroups), $matches[1save the result information of the second time, etc. 
PREG_OFFSET_CAPTUREViewPREG_SPLIT_OFFSET_CAPTUREdescription.4.3.0
PREG_SPLIT_NO_EMPTYthis marker tells preg_split() to return non-empty parts. 
PREG_SPLIT_DELIM_CAPTUREthis marker tells preg_split() to capture the content of bracket expressions at the same time.4.0.5
PREG_SPLIT_OFFSET_CAPTUREif this marker is set, the offset of each match substring will also be returned. Note that this will change the values in the returned array,     each element is a match substring as the 0th element, and its offset relative to the target string as the1an array of elements. This     the marker can only be used for preg_split().4.3.0
PREG_NO_ERRORpreg_last_error() is returned when there is no matching error.5.2.0
PREG_INTERNAL_ERRORif there is an internal error in PCRE, preg_last_error() is returned.5.2.0
PREG_BACKTRACK_LIMIT_ERRORif the backtracking limit exceeds, preg_last_error() is returned when called.    5.2.0
PREG_RECURSION_LIMIT_ERRORif the recursion limit exceeds, preg_last_error() is returned when called.5.2.0
PREG_BAD_UTF8_ERRORif the last error is due to an abnormal utf-8data (only when running in UTF-8 available under the pattern regular expression). Caused by, called by preg_last_error().5.2.0
PREG_BAD_UTF8_OFFSET_ERRORif the offset is not equal to a valid urf-8code does not match (only when running in UTF-8 available under the pattern regular expression). Called by preg_last_error().5.3.0
PCRE_VERSIONPCRE version number and release date (for example: ")7.0 18-Dec-2006").5.2.4