Page 604 - Beginning PHP 5.3
        P. 604
     Part III: Using PHP in Practice
                        ((-|\.)[A-Za-z\d]+)*  # followed 0 or more times by (- or . and more
                                              # alphanums)
                        \.[A-Za-z\d]+         # followed by a final dot and some alphanumerics
                        $                     # End of string
                        /x”;
                      $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”;
                      $productCodePattern = “/^(SW|MW|WW)(\d{2})$/i”;
                      if ( !preg_match( $emailAddressPattern, $_POST[“emailAddress”] ) )
                    $errorMessages[] = “Invalid email address”;
                      if ( !preg_match( $phoneNumberPattern, $_POST[“phoneNumber”] ) )
                    $errorMessages[] = “Invalid phone number”;
                      if ( $errorMessages ) {
                        echo “<p>There was a problem with the form you sent:</p><ul>”;
                        foreach ( $errorMessages as $errorMessage ) echo “<li>$errorMessage
                    </li>”;
                        echo ‘<p>Please <a href=”javascript:history.go(-1)”>go back</a> and try
                    again.</p>’;
                      } else {
                        echo “<p>Thanks for your order! You ordered the following items:</
                    p><ul>”;
                        $productCodes = preg_split( “/\W+/”, $_POST[“productCodes”], -1, PREG_
                    SPLIT_NO_EMPTY );
                        $products = preg_replace_callback( $productCodePattern,
                    “expandProductCodes”, $productCodes );
                        foreach ( $products as $product ) echo “<li>$product</li>”;
                        echo “</ul>”;
                      }
                    }
                    function expandProductCodes( $matches ) {
              566
                                                                                                      9/21/09   6:18:02 PM
          c18.indd   566
          c18.indd   566                                                                              9/21/09   6:18:02 PM
     	
