Page 600 - Beginning PHP 5.3
P. 600

Part III: Using PHP in Practice
                    $text = “Andy scored 184 points, Rachel attained 198 points and Bert scored
                    112 points.”;

                    $pattern = “/
                      (Andy|Rachel|Bert)\   # Only match people we know about
                      (scored|attained)\    # Two words, same meaning
                      (\d+)                 # The number of points scored
                    /x”;

                    preg_match_all( $pattern, $text, $matches );

                    for ( $i = 0; $i  <  count( $matches[0] ); $i++ ) {
                      echo $matches[1][$i] . “: “ . $matches[3][$i] . “ < br / > ”;
                    }

                   This code produces the following output:

                    Andy: 184
                    Rachel: 198

                    Bert: 112
                   Finally, here ’ s an example that uses the  e  modifier. This is the same example used in the
                 preg_replace_callback()  section earlier in the chapter, rewritten to use  e  instead:

                    $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.”;

                    echo preg_replace( “/\\$(\d+\.\d{2})/e”, “’$’ . ($1 + 1)”, $text );

                   For each match, the PHP code within the replacement string displays a dollar symbol followed by the
                 text from the subpattern match (the price) plus one. This results in the following output:

                    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.

                   You can combine several modifiers at once  —  just add the modifier letters one after the other:
                    $text = “Hello, World!\nHow are you today?\n”;
                    echo preg_match( “/world!$/im”, $text ) . “ < br / > ”;  // Displays “1”





                      You can see the full list of pattern modifiers at  http://www.php.net/manual/en/reference
                    .pcre.pattern.modifiers.php  .
                  Splitting a String with a Regular
              Expression

                   The final regular expression function explored in this chapter is  preg_split() . In Chapter 6 you
                studied the   explode()  function, which allows you to split a string into an array of substrings.
                You pass in a delimiter string (a comma, for example) and the target string is split at each place the
                delimiter is found.
              562





                                                                                                      9/21/09   6:18:00 PM
          c18.indd   562
          c18.indd   562                                                                              9/21/09   6:18:00 PM
   595   596   597   598   599   600   601   602   603   604   605