Specifications

We can adapt the Smart Form example to use regular expressions as follows:
if (!eregi(“^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$”, $email))
{
echo “That is not a valid email address. Please return to the”
.” previous page and try again.”;
exit;
}
$toaddress = “feedback@bobsdomain.com”; // the default value
if (eregi(“shop|customer service|retail”, $feedback))
$toaddress = “retail@bobsdomain.com”;
else if (eregi(“deliver.*|fulfil.*”, $feedback))
$toaddress = “fulfilment@bobsdomain.com”;
else if (eregi(“bill|account”, $feedback))
$toaddress = “accounts@bobsdomain.com”;
if (eregi(“bigcustomer\.com”, $email))
$toaddress = “bob@bobsdomain.com”;
Replacing Substrings with Regular Expressions
You can also use regular expressions to find and replace substrings in the same way as we used
str_replace(). The two functions available for this are ereg_replace() and
eregi_replace(). The function ereg_replace() has the following prototype:
string ereg_replace(string pattern, string replacement, string search);
This function searches for the regular expression pattern in the search string and replaces it
with the string replacement.
The function eregi_replace() is identical, but again, is not case sensitive.
Splitting Strings with Regular Expressions
Another useful regular expression function is split(), which has the following prototype:
array split(string pattern, string search, int [max]);
This function splits the string search into substrings on the regular expression pattern and
returns the substrings in an array. The max integer limits the number of items that can go into
the array.
This can be useful for splitting up domain names or dates. For example
$domain = “yallara.cs.rmit.edu.au”;
$arr = split (“\.”, $domain);
String Manipulation and Regular Expressions
C
HAPTER 4
4
S
TRING
M
ANIPULATION
115
06 7842 CH04 3/6/01 3:41 PM Page 115