PHP Regular Expressions (PCRE)

A regular expression describes a string matching pattern. It can be used to check whether a string contains a certain substring, replace matched substrings, or extract substrings that meet certain conditions from a string.

For more regular expression content, please refer to our:Regular Expressions - Tutorial。

In PHP, regular expressions are mainly used through a set of built-in functions, the most commonly used of which are:

  • preg_match(): Searches for a matching pattern in a string and returns only the first match.
  • preg_match_all(): Searches for a matching pattern in a string and returns all matches.
  • preg_replace(): Searches for a matching pattern in a string and then replaces it.

preg_match() function

The preg_match() function is used to search for a matching pattern in a string and returns only the first match.

preg_match(pattern, subject, matches)

Parameter description:

  • pattern: The regular expression pattern.
  • subject: The string to search.
  • matches: Optional parameter used to store the matching result.

Example

<?php
$str = "Hello, World!";
$pattern = "/Hello/";
if (preg_match($pattern, $str, $matches)) {
    echo "Match successful!" . PHP_EOL;
    print_r($matches);
} else {
    echo "No match found!";
}
?>

The output of the above code is:

匹配成功!
Array
(
    [0] => Hello
)

preg_match_all() function

The preg_match_all() function is used to search for a matching pattern in a string and returns all matches.

preg_match_all(pattern, subject, matches)

Parameter description:

  • Used to search for a matching pattern in a string and return all matches.
  • pattern: The regular expression pattern.
  • subject: The string to search.
  • matches: Array used to store all matching results.

Example

<?php
$str = "The cat and the hat";
$pattern = "/[aeiou]/";
if (preg_match_all($pattern, $str, $matches)) {
    echo "Matching vowels: " . implode(", ", $matches[0]);
} else {
    echo "No match found!";
}
?>

The output of the above code is:

匹配的元音字母: e, a, a, e, a

preg_replace() function

The preg_replace() function is used to search for a matching pattern in a string and then replace it.

preg_replace(pattern, replacement, subject)

Parameter description:

  • pattern: The regular expression pattern.
  • replacement: The replacement string.
  • subject: The string to search.

Example

<?php
$str = "I love PHP";
$pattern = "/PHP/";
$replacement = "EXAMPLE";
$new_str = preg_replace($pattern, $replacement, $str);
echo "String after replacement: " . $new_str;
?>

The output of the above code is:

替换后的字符串: I love EXAMPLE

PCRE Functions

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

FunctionDescription
preg_filter Perform a regular expression search and replace
preg_grep Return array entries that match the pattern
preg_last_error Return the error code generated by 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 using a regular expression

PREG Constants

Constant Description Since which version
PREG_PATTERN_ORDER Results are ordered by "pattern", only used for preg_match_all(), i.e., $matches since
PREG_SET_ORDER Results are ordered by "set", only used for preg_match_all(), i.e., $matches  
PREG_OFFSET_CAPTURE SeePREG_SPLIT_OFFSET_CAPTUREthe description of. 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 occurrence of the matched substring will also be returned. Note that this changes the values in the returned array, where each element is an array consisting of the matched substring as element 0 and its offset relative to the target string as element 1. This flag can only be used with preg_split(). 4.3.0
PREG_NO_ERROR Returned by preg_last_error() when there is no matching error. 5.2.0
PREG_INTERNAL_ERROR Returned by preg_last_error() when there is a PCRE internal error. 5.2.0
PREG_BACKTRACK_LIMIT_ERROR Returned by preg_last_error() when the backtracking 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() if the last error was caused by abnormal UTF-8 data (only available when running in UTF-8 mode regular expressions). 5.2.0
PREG_BAD_UTF8_OFFSET_ERROR Returned by preg_last_error() if the offset does not match 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
Other Extensions