Regular expression functions
Regular expression functions
ereg
Name
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:
Example 1. 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
Name
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.
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. For example, the following code snippet prints "This was a test" three times:
eregi
Example 1. 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.
Name
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
Name
eregi_replace — replace regular expression case insensitive
Description
split
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.
Name
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:
Example 1. split() example
$passwd_list = split( ":", $passwd_line, 5 );
sql_regcase
Name
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.
Example 1. 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.