Page 608 - Beginning PHP 5.3
P. 608

Part III: Using PHP in Practice
                   If one or more error messages were generated, they are displayed to the user:

                      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 > ’;

                   If all was well with the form, a thank - you message is displayed, and the list of ordered products is
                 shown to the user in expanded form:

                      } 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 > ”;
                  First,  preg_split()  is used to split the supplied product code string into an array of individual product
                codes. The delimiter is a string of one or more non - word characters (  \W+ ). This allows a degree of
                flexibility; for example, the user can use a comma to separate the codes, or a comma followed by a space,
                or a hyphen.

                  Next the array of product codes is passed to   preg_replace_callback()  to turn them into an
                 array of product names (  $products ). The product code regular expression created earlier
                 (  $productCodePattern ) is used to match the two portions of the product code. The expansion is
                 handled by the   expandProductCodes()  function, which is explained in a moment.

                   Finally, the function loops through the   $products  array, displaying the product names in an
                unordered list.

                 The   expandProductCodes()  function defines an array to map the two - letter portion of the product
                 code to a product range:
                      $productCodes = array(
                        “SW” = >  “SuperWidget”,
                        “MW” = >  “MegaWidget”,
                        “WW” = >  “WonderWidget”

                      );
                  Then it ’ s simply a case of using the array to convert the first subpattern match  —  $matches[1]   —  to the

                product range string, then returning this string, followed by the string  “  model # ”,  followed by the
                second subpattern match, which is the two - digit product code:


                      return $productCodes[$matches[1]] . “ model #” . $matches[2];





              570





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