PHP Regular Expressions (PCRE)
A regular expression describes a pattern for string matching; it can be used to check whether a string contains a certain substring, replace a matched substring, or extract a substring that meets certain conditions from a string.
For more on regular expressions, refer to our:Regular Expressions - Tutorial.
Using regular expressions in PHP is mainly done through a set of built-in functions, the most commonly used being:
preg_match(): Searches for a matching pattern in a string, returning only the first match.preg_match_all(): Searches for a matching pattern in a string, returning all matches.preg_replace(): Searches for a matching pattern in a string, then performs a replacement.
preg_match() Function
The preg_match() function is used to search for a matching pattern in a string, returning only the first match.
preg_match(pattern, subject, matches)
Parameter Description:
- pattern: the regular expression pattern.
- subject: the string to search.
- matches: an optional parameter used to store the matching results.
Example
The output of the code above is:
匹配成功!
Array
(
[0] => Hello
)
preg_match_all() Function
The preg_match_all() function is used to search for a matching pattern in a string, returning all matches.
preg_match_all(pattern, subject, matches)
Parameter Description:
- Used to search for a matching pattern in a string, returning all matches.
pattern: the regular expression pattern.subject: the string to search.matches: an array used to store all matching results.
Example
The output of the code above is:
匹配的元音字母: e, a, a, e, a
preg_replace() Function
The preg_replace() function is used to search for a matching pattern in a string, then performs a replacement.
preg_replace(pattern, replacement, subject)
Parameter Description:
- pattern: the regular expression pattern.
- replacement: the replacement string.
- subject: the string to search.
Example
The output of the code above is:
替换后的字符串: I love RUNOOB
PCRE Functions
In PHP we can use the PCRE extension to match patterns in strings.
| function | Description |
|---|---|
| preg_filter | Perform a regular expression search and replace |
| preg_grep | Returns an array entry of matched patterns |
| preg_last_error | Returns the error code from the last PCRE regex execution |
| preg_match_all | Perform a global regular expression match |
| preg_match | Perform a regular expression match |
| preg_quote | Escape regular expression characters |
| preg_replace_callback_array | Perform a regular expression search and replace using a callback |
| 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 a string by a regular expression |
PREG Constants
| Constant | Description | Since Which Version |
|---|---|---|
PREG_PATTERN_ORDER |
Results are sorted by "rule", used only for preg_match_all(), i.e., $matches[0] is the full rule match result, $matches[1] is the first subgroup match result, and so on. | since |
PREG_SET_ORDER |
Results are sorted by "set", used only for preg_match_all(), i.e., $matches[0] holds all results of the first match (including subgroups), $matches[1] holds the second match's results, and so on. | |
PREG_OFFSET_CAPTURE |
View **PREG_SPLIT_OFFSET_CAPTURE** description. |
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 the content matched by parenthesized expressions. | 4.0.5 |
PREG_SPLIT_OFFSET_CAPTURE |
If this flag is set, the offset of each matched substring is also returned. Note that this changes the values in the returned array; each element is an array with the matched substring as the 0th element and its offset relative to the target string as the 1st element. This flag can only be used with preg_split(). | 4.3.0 |
PREG_NO_ERROR |
Returned when preg_last_error() is called with no matching error. | 5.2.0 |
PREG_INTERNAL_ERROR |
Returned when preg_last_error() is called if there is an internal PCRE error. | 5.2.0 |
PREG_BACKTRACK_LIMIT_ERROR |
Returned when preg_last_error() is called if the backtrack limit is exceeded. | 5.2.0 |
PREG_RECURSION_LIMIT_ERROR |
Returned when preg_last_error() is called if the recursion limit is exceeded. | 5.2.0 |
PREG_BAD_UTF8_ERROR |
Returned when preg_last_error() is called if the last error was due to abnormal UTF-8 data (only available when running in UTF-8 mode regular expressions). | 5.2.0 |
PREG_BAD_UTF8_OFFSET_ERROR |
Returned when preg_last_error() is called if the offset does not match a valid UTF-8 code (only available when running in UTF-8 mode regular expressions). | 5.3.0 |
PCRE_VERSION |
PCRE version number and release date (e.g.: "7.0 18-Dec-2006"). | 5.2.4 |