PHP Regular Expressions
Global RegExp match
A global RegExp match can be performed using preg_match_all. preg_match_all returns all matching results in the subject string (in contrast to preg_match, which only returns the first one).
The 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 PREG_SET_ORDER and PREG_PATTERN_ORDER.
Following code demonstrates usage of preg_match_all:
$matches has three nested arrays. Each array represents one match, which has the same format as the return result of preg_match.
The second var_dump (PREG_PATTERN_ORDER) gives this output:
When the same regexp is run through preg_match, following array is returned:
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 b.
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 replacing with regular expression
Searches for everything between semicolons and reverses the order.
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.