Regular Expressions (regexp/PCRE)
preg_replace($pattern, $replacement, $subject, $limit = -1, $count = 0);
preg_replace_callback($pattern, $callback, $subject, $limit = -1, $count = 0);
preg_match($pattern, $subject, &$matches, $flags = 0, $offset = 0);
preg_match_all($pattern, $subject, &$matches, $flags = PREG_PATTERN_ORDER, $offset = 0);
preg_split($pattern, $subject, $limit = -1, $flags = 0)
|a string with a regular expression (PCRE pattern)|
PHP regular expressions follow PCRE pattern standards, which are derived from Perl regular expressions.
All PCRE strings in PHP must be enclosed with delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character. Popular delimiters are
% for instance.
PCRE patterns can contain groups, character classes, character groups, look-ahead/look-behind assertions and escaped characters.
It is possible to use PCRE modifiers in the
$pattern string. Some common ones are
i (case insensitive),
m (multiline) and
s (the dot metacharacter includes newlines). The
g (global) modifier is not allowed, you will use the
preg_match_all function instead.
Matches to PCRE strings are done with
$ prefixed numbered strings:
Global RegExp match
A global RegExp match can be performed using
preg_match_all returns all matching results in the subject string (in contrast to
preg_match, which only returns the first one).
preg_match_all function returns the number of matches. Third parameter
$matches will contain matches in format controlled by flags that can be given in fourth parameter.
If given an array,
$matches will contain array in similar format you’d get with
preg_match, except that
preg_match stops at first match, where
preg_match_all iterates over the string until the string is wholly consumed and returns result of each iteration in a multidimensional array, which format can be controlled by the flag in fourth argument.
The fourth argument,
$flags, controls structure of
$matches array. Default mode is
PREG_PATTERN_ORDER and possible flags are
Following code demonstrates usage of
The first var_dump from
PREG_SET_ORDER gives this output:
$matches has three nested arrays. Each array represents one match, which has the same format as the return result of
The second var_dump (
PREG_PATTERN_ORDER) gives this output:
When the same regexp is run through
preg_match, following array is returned:
Split string into array by a regular expression
To split a string into a array simply pass the string and a regexp for
preg_split(); to match and search, adding a third parameter (
limit) allows you to set the number of "matches" to perform, the remaining string will be added to the end of the array.
The fourth parameter is (
flags) here we use the
PREG_SPLIT_NO_EMPTY which prevents our array from containing any empty keys / values.
String matching with regular expressions
preg_match checks whether a string matches the regular expression.
If you pass in a third parameter, it will be populated with the matching data of the regular expression:
$matches contains an array of the whole match then substrings in the regular expression bounded by parentheses, in the order of open parenthesis's offset. That means, if you have
/z(a(b))/ as the regular expression, index 0 contains the whole substring
zab, index 1 contains the substring bounded by the outer parentheses
ab and index 2 contains the inner parentheses
String replace with callback
preg_replace_callback works by sending every matched capturing group to the defined callback and replaces it with the return value of the callback.
This allows us to replace strings based on any kind of logic.
String replacing with regular expression
Searches for everything between semicolons and reverses the order.