Regular expression functions
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
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.
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 carriage return
characters in $string. */
ereg
regular expression match
Description
int ereg
string pattern
string string
array regs
Searchs 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
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.
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 );
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 save 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 save 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 pattern. If an error occurs, returns false.
To get the first five fields from a line from
/etc/passwd:
split() example
$passwd_list = split( ":", $passwd_line, 5 );
Note that pattern is case-sensitive.
See also: 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.