Page 598 - Beginning PHP 5.3
P. 598

Part III: Using PHP in Practice
                  Replacing Text using a Callback Function

                    preg_replace()  is a powerful, flexible function, offering a multitude of ways to search and replace
                 text. However, if you need even more flexibility you can use   preg_replace_callback() , which lets
                you create a callback function to handle the replacement side of the operation.

                   preg_replace_callback()  works in much the same way as  preg_replace() , and accepts all the
                 same arguments, except that instead of passing a replacement string (or array of strings) as the second
                 argument, you pass the name of your callback function as a string.

                   Your callback function needs to accept an array of matches. The first element of the array (at index   0 )
                 contains the whole matched text, and additional elements contain any matched subpatterns. The string
                 that your function returns is then used as the replacement text.
                   Here ’ s an example. Say you have a large amount of sales copy that mentions prices of various products in
                 your online store, and you want to increase all your product prices by a dollar. You can ’ t do arithmetic
                 in regular expressions, but you can use   preg_replace_callback()  and a callback function to add
                 numbers together:
                    $text = “Our high-quality mouse mat is just $3.99,
                    while our keyboard covers sell for $4.99 and our
                    screen protectors for only $5.99.”;

                    function addADollar( $matches ) {
                      return “$” . ( $matches[1] + 1 );
                    }

                    echo preg_replace_callback( “/\\$(\d+\.\d{2})/”, “addADollar”, $text );

                  The  addADollar()  callback function takes the second element in the matches array, which contains the
                matched text from the subpattern in the regular expression (that is, the price without the dollar symbol),
                and adds one to it. It returns this new value, preceded by a dollar symbol. This string is then used by
                  preg_replace_callback()  to replace the matched text, producing the following result:

                    Our high-quality mouse mat is just $4.99, while our keyboard covers sell for
                    $5.99 and our screen protectors for only $6.99.



                  Altering Matching Behavior with

              Pattern Modifiers
                   By placing a single letter, known as a  pattern modifier , directly after the closing delimiter of a regular
                expression, you can change the way that the expression behaves. Here ’ s a list of the more useful
                modifiers:









              560





                                                                                                      9/21/09   6:18:00 PM
          c18.indd   560
          c18.indd   560                                                                              9/21/09   6:18:00 PM
   593   594   595   596   597   598   599   600   601   602   603