Page 607 - Beginning PHP 5.3
P. 607

Chapter 18: String Matching with Regular Expressions
                           The expression has been laid out in an easy - to - read format by using the  x  pattern modifier. The
                         comments help to make the expression self - explanatory. Essentially, in order to match the expression,
                         the email address needs to consist of a name portion, followed by an @ (at) symbol, followed by a
                         domain portion.

                           The name portion should be a string of letters and/or digits. The string may optionally contain hyphens,
                         dots, or underscores; however, the name mustn ’ t begin or end with a hyphen or dot.

                           The domain portion needs to start with a string of letters and/or digits, which may optionally contain
                         hyphens or dots, and finish with a final dot and more letters and/or digits (for example,  “ .com ” ).

                           Next, the function defines a regular expression to validate a U.S. phone number:

                               $phoneNumberPattern = “/
                                 ^                     # Start of string

                                 (                     # Optional area code followed by optional
                                                       # separator:
                                   \(\d{3}\)[-. ]?     # Code with parentheses
                                   |                   # or
                                   \d{3}[-. ]?         # Code without parentheses
                                 )?

                                 \d{3}                 # Prefix
                                 [-.]                  # Hyphen or dot separator
                                 \d{4}                 # Line number

                                 $                     # End of string

                                 /x”;
                           A U.S. phone number can consist of an optional three - digit area code, followed by an optional hyphen,
                         dot, or space, followed by the three - digit prefix, then a hyphen or dot, then the four - digit line number.
                         The expression can deal with area codes surrounded by parentheses  —  such as (599) 123 - 4567  —  as well
                         as area codes without parentheses  —  for example: 599 - 123 - 4567.

                           The function also defines a regular expression that matches a valid product code  —  this is used to
                         convert the product codes into product names:

                               $productCodePattern = “/^(SW|MW|WW)(\d{2})$/i”;
                           A product code is simply  “ SW ”  ,   “ MW ”,  or  “ WW ”,  followed by a two - digit number. Notice that both
                         portions of the product code are matched using subpatterns so that the matched values can be extracted.

                           Now the function validates the supplied email address and phone number. If either of them fail to
                         match, an error message is generated:
                               if ( !preg_match( $emailAddressPattern, $_POST[“emailAddress”] ) )
                             $errorMessages[] = “Invalid email address”;
                               if ( !preg_match( $phoneNumberPattern, $_POST[“phoneNumber”] ) )

                             $errorMessages[] = “Invalid phone number”;



                                                                                                         569





                                                                                                      9/21/09   6:18:04 PM
          c18.indd   569                                                                              9/21/09   6:18:04 PM
          c18.indd   569
   602   603   604   605   606   607   608   609   610   611   612