String Manipulation and Regular Expressions CHAPTER 4 115 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@”; // the default value if (eregi(“shop|customer service|retail”, $feedback)) $toaddress = “retail@”; else if (eregi(“deliver.*|fulfil.*”, $feedback)) $toaddress = “fulfilment@”; else if (eregi(“bill|account”, $feedback)) $toaddress = “accounts@”; if (eregi(“bigcustomer\.com”, $email)) $toaddress = “bob@”; 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. | 115 String Manipulation and Regular Expressions Chapter 4 We can adapt the Smart Form example to use regular expressions as follows if eregi A 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@ the default value if eregi shop customer service retail feedback toaddress retail@ else if eregi deliver. lfulfil. feedback toaddress fulfilment@ else if eregi bill account feedback toaddress accounts@ if eregi bigcustomer .com email toaddress bob@ 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 arr split . domain 4 String Manipulation 116 Using PHP Part I while list key value each arr echo br . value This splits the host name into its five components and prints each on a separate line. Comparison of String Functions and Regular Expression Functions In general the regular expression functions run less efficiently than the string .