# Perl-Compatible Regular Expressions - PHP

Andrei Zmievski added support to PHP for Perl-compatible regular expressions. Expressions are surrounded by delimiters, which are usually / or | characters, but can be any printable character other than a number, letter, or backslash. After the second delimiter, you may place one or more modifiers. These are letters that change the way the regular expression is interpreted.

For the most part, the functions in this section comply with the way regular expressions work in Perl 5. There are a few very specific differences. They are narrow enough that you probably won't run into them, and they may not make much sense without explaining regular expressions in detail.

array preg_grep(string pattern, array data)
Core PHP Programming
IT-SC book 286
The preg_grep function compares the elements of the data argument that match the given pattern.

boolean preg_match(string pattern, string text, array matches)
The preg_match function is the equivalent of ereg. It evaluates the pattern argument as a regular expression and attempts to find matches in the text argument. If the optional matches argument is supplied, each match will be added to the array. TRUE is returned if at least one match is made, FALSE otherwise.

The first element in the matches array, with an index of zero, will contain the match for the entire regular expression. Subsequent elements of matches will contain the matches for subexpressions. These are the expressions enclosed in parentheses in the example.

<?
// show User Agent
print("User Agent: $HTTP_USER_AGENT<BR> "); // try to parse User Agent if(preg_match("/^(.+)/([0-9]).([0-9]+)/",$HTTP_USER_AGENT, $matches)) { print("Full match:$matches[0]<BR> ");
print("Browser: $matches[1]<BR> "); print("Major Version:$matches[2]<BR> ");
print("Minor Version: $matches[3]<BR> "); } else { print("User Agent not recognized"); } ?> integer preg_match_all (string pattern, string text, array matches, integer order) The preg_match_all function operates similarly to preg_match. A pattern is evaluated against the text argument, but instead of stopping when a match is found, subsequent matches are sought. The matches argument is required and will receive a two-dimensional array. The method for filling this array is determined by the order argument. It may be set with two constants, either PREG_PATTERN_ORDER, the default, or PREG_SET_ORDER. The number of matches against the full pattern is returned. preg_match_all If PREG_PATTERN_ORDER is used, the first element of the matches array will contain an array of all the matches against the full pattern. The other elements of the array will contain arrays of matches against subpatterns. If PREG_SET_ORDER is used, each element of the matches array contains an array organized like those created by preg_match. The first element is the entire matching string. Each subsequent element contains the match against the subpattern for that match. string preg_quote(string text) The preg_quote function returns text with backslashes inserted before character that have special meaning to the functions in this section. The special characters are: . + * ? [ ^ ]$ ( ) { } = ! < > | :

string preg_replace(string pattern, string replacement, string text)
The preg_replace function is equivalent to ereg_replace. Each time the pattern matches a substring within the text argument, it is replaced with the replacement argument. The text argument is unchanged, but the altered version is returned. If the pattern contains subexpressions in parentheses, the replacement argument may contain a special code for specifying which subexpression to replace. The form is to use two backslashes followed by a single digit, zero through nine. Zero matches the entire expression; one through nine each match the first nine subexpressions, respectively. Subexpressions are numbered left to right, which accounts for nested subexpressions.

<?
// swap newlines for break tags
$text = "line1 line2 line3 "; print(preg_replace("| |", "<BR>",$text));
print("<HR> ");
//mix up these words
$text = "one two three four"; print(preg_replace("|([a-z]+) ([a-z]+) ([a-z]+) ([a-z]+)|", "4 2 1 3",$text));
?>

array preg_split(string pattern, string text, integer limit)
The preg_split function returns an array of substrings from the text argument. The pattern argument will be used as a field delimiter. The optional limit argument sets the maximum number of elements to return. This function is equivalent to split.

<?
$paragraph = "This is a short paragraph. Each ";$paragraph .= "sentence will be extracted by ";
$paragraph .= "the preg_split function. As a ";$paragraph .= "result, you will be amazed!";
$sentence = preg_split("/[.!?]/",$paragraph);
for($index = 0;$index < count($sentence);$index++)
{
print("$index.$sentence[\$index] <BR> ");
}
?>