Regular Expression Functions (POSIX Extended)
Regexps
Regular expressions are used for complex string manipulation in
PHP. The functions that support regular expressions are:
ereg
ereg_replace
eregi
eregi_replace
split
spliti
These functions all take a regular expression string as their
first argument. PHP uses the POSIX extended regular expressions
as defined by POSIX 1003.2. For a full description of POSIX
regular expressions see the regex man pages included in the regex
directory in the PHP distribution. It's in manpage format, so
you'll want to do something along the lines of man
/usr/local/src/regex/regex.7 in order to read it.
Regular Expression Examples
ereg ("abc", $string);
/* Returns true if "abc"
is found anywhere in $string. */
ereg ("^abc", $string);
/* Returns true if "abc"
is found at the beginning of $string. */
ereg ("abc$", $string);
/* Returns true if "abc"
is found at the end of $string. */
eregi ("(ozilla.[23]|MSIE.3)", $HTTP_USER_AGENT);
/* Returns true if client browser
is Netscape 2, 3 or MSIE 3. */
ereg ("([[:alnum:]]+) ([[:alnum:]]+) ([[:alnum:]]+)", $string,$regs);
/* Places three space separated words
into $regs[1], $regs[2] and $regs[3]. */
$string = ereg_replace ("^", "<BR>", $string);
/* Put a <BR> tag at the beginning of $string. */
$string = ereg_replace ("$", "<BR>", $string);
/* Put a <BR> tag at the end of $string. */
$string = ereg_replace ("\n", "", $string);
/* Get rid of any newline
characters in $string. */
ereg
Regular expression match
Description
int ereg
string pattern
string string
array
regs
Searches a string for matches to the regular
expression given in pattern.
If matches are found for parenthesized substrings of
pattern and the function is called with
the third argument regs, the matches will
be stored in the elements of the array
regs. $regs[1] will contain the substring
which starts at the first left parenthesis; $regs[2] will contain
the substring starting at the second, and so on. $regs[0] will
contain a copy of string.
If ereg finds any matches at all, $regs will
be filled with exactly ten elements, even though more or fewer
than ten parenthesized substrings may actually have matched.
This has no effect on ereg's ability to
match more substrings. If no matches are found, $regs will not be
altered by ereg.
Searching is case sensitive.
Returns true if a match for pattern was
found in string, or false if no matches
were found or an error occurred.
The following code snippet takes a date in ISO format
(YYYY-MM-DD) and prints it in DD.MM.YYYY format:
Ereg Example
if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
echo "$regs[3].$regs[2].$regs[1]";
} else {
echo "Invalid date format: $date";
}
See also eregi,
ereg_replace, and
eregi_replace.
ereg_replace
Replace regular expression
Description
string ereg_replace
string pattern
string replacement
string string
This function scans string for matches to
pattern, then replaces the matched text
with replacement.
The modified string is returned. (Which may mean that the
original string is returned if there are no matches to be
replaced.)
If pattern contains parenthesized
substrings, replacement may contain
substrings of the form
\\digit, which will
be replaced by the text matching the digit'th parenthesized
substring; \\0 will produce the entire
contents of string. Up to nine substrings may be used.
Parentheses may be nested, in which case they are counted by the
opening parenthesis.
If no matches are found in string, then
string will be returned unchanged.
For example, the following code snippet prints "This was a test"
three times:
Ereg_replace Example
$string = "This is a test";
echo ereg_replace (" is", " was", $string);
echo ereg_replace ("( )is", "\\1was", $string);
echo ereg_replace ("(( )is)", "\\2was", $string);
One thing to take note of is that if you use an integer value as
the replacement parameter, you may not get
the results you expect. This is because
ereg_replace will interpret the number as
the ordinal value of a character, and apply that. For instance:
ereg_replace Example
<?php
/* This will not work as expected. */
$num = 4;
$string = "This string has four words.";
$string = ereg_replace('four', $num, $string);
echo $string; /* Output: 'This string has words.' */
/* This will work. */
$num = '4';
$string = "This string has four words.";
$string = ereg_replace('four', $num, $string);
echo $string; /* Output: 'This string has 4 words.' */
?>
See also ereg, eregi,
and eregi_replace.
eregi
case insensitive regular expression match
Description
int eregi
string pattern
string string
array
regs
This function is identical to ereg except
that this ignores case distinction when matching alphabetic
characters.
See also ereg,
ereg_replace, and
eregi_replace.
eregi_replace
replace regular expression case insensitive
Description
string eregi_replace
string pattern
string replacement
string string
This function is identical to ereg_replace
except that this ignores case distinction when matching
alphabetic characters.
See also ereg, eregi,
and ereg_replace.
split
split string into array by regular expression
Description
array split
string pattern
string string
int
limit
Returns an array of strings, each of which is a substring of
string formed by splitting it on
boundaries formed by the regular expression
pattern. If limit
is set, the returned array will contain a maximum of
limit elements with the last element
containing the whole rest of string. If
an error occurs, split returns false.
To get the first five fields from a line from
/etc/passwd:
Split Example
$passwd_list = split (":", $passwd_line, 5);
To parse a date which may be delimited with slashes, dots, or
hyphens:
Split Example
$date = "04/30/1973"; // Delimiters may be slash, dot, or hyphen
list ($month, $day, $year) = split ('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year<br>\n";
Note that pattern is case-sensitive.
Note that if you don't require the power of regular expressions,
it is faster to use explode, which doesn't
incur the overhead of the regular expression engine.
Please note that pattern is a regular
expression. If you want to split on any of the characters which
are considered special by regular expressions, you'll need to
escape them first. If you think split (or
any other regex function, for that matter) is doing something
weird, please read the file regex.7,
included in the regex/ subdirectory of the
PHP distribution. It's in manpage format, so you'll want to do
something along the lines of man
/usr/local/src/regex/regex.7 in order to read it.
See also: spliti,
explode, and implode.
spliti
Split string into array by regular expression case insensitive
Description
array split
string pattern
string string
int
limit
This function is identical to split except
that this ignores case distinction when matching alphabetic
characters.
See also: split,
explode, and implode.
sql_regcase
Make regular expression for case insensitive match
Description
string sql_regcase
string string
Returns a valid regular expression which will match
string, ignoring case. This expression is
string with each character converted to a
bracket expression; this bracket expression contains that
character's uppercase and lowercase form if applicable, otherwise
it contains the original character twice.
Sql_regcase Example
echo sql_regcase ("Foo bar");
prints [Ff][Oo][Oo][ ][Bb][Aa][Rr].
This can be used to achieve case insensitive pattern matching in
products which support only case sensitive regular expressions.