[Previous] [Contents] [Next]

Regular Expression Functions

PHP has several functions that use POSIX regular expressions to find and extract substrings, replace substrings, and split a string into an array. The functions to perform these tasks come in pairs: a case-sensitive version and a case-insensitive version. While case-sensitive regular expressions can be written, the case-insensitive versions of these functions allow shorter regular expressions.

Finding and extracting values

The ereg( ) function, and the case-insensitive version eregi( ), are defined as:

boolean ereg(string pattern, string subject [, array var])
boolean eregi(string pattern, string subject [, array var])

Both functions return true if the regular expression pattern is found in the subject string. An optional array variable var can be passed as the third argument; it is populated with the portions of subject that are matched by up to nine grouped subexpressions in pattern. Both functions return false if the pattern isn't found in the subject.

To extract values from a string into an array, patterns can be arranged in groups contained by parentheses in the regular expression. The following example shows how the year, month, and day components of a date can be extracted into an array:

$parts = array(  );
$value = "2001-09-07";
$pattern = '^([0-9]{4})-([0-9]{2})-([0-9]{2})$';
ereg($pattern, $value, $parts);
// Array ([0]=> 2001-09-07 [1]=>2001 [2]=>09 [3]=>07

The expression:


matches dates in the format YYYY-MM-DD. After calling ereg( ), $parts[0] is assigned the portion of the string that matches the whole regular expression-in this case, the whole string 2001-09-07. The portion of the date that matches each group in the expression is assigned to the following array elements: $parts[1] contains the year matched by ([0-9]{4}), $parts[2] contains the month matched by ([0-9]{2}), and $parts[3] contains the day matched by "([0-9]{2})".

[Previous] [Contents] [Next]