Page 579 - Beginning PHP 5.3
P. 579

Chapter 18: String Matching with Regular Expressions
                           You can see that, whereas  strstr()  can match only the exact string passed to it, regular expressions can
                          contain a series of  “ rules ”  for creating quite complex matching patterns.



                           Pattern Matching in PHP

                          PHP ’ s main pattern - matching function is  preg_match() . This function takes the following arguments:

                            ❑       The regular expression to search for (as a string)
                            ❑       The string to search through
                            ❑       An optional array to store any matched text in. (The matched text is stored in the first element.)
                            ❑       An optional integer specifying any flags for the match operation. Currently only one flag is
                                supported:   PREG_OFFSET_CAPTURE . Pass this constant to get  preg_match()  to return the
                                position of any match in the array as well as the text matched. (If you need to pass a fifth
                                argument to   preg_match()  and you want to turn off this feature, specify a value of zero
                                instead.)
                            ❑       An optional integer offset from the start of the string (the first character has an offset of zero, the
                                second character has an offset of 1, and so on). If specified,   preg_match()  starts the search from
                                this position in the string, rather than from the first character
                            preg_match()  returns zero if the pattern didn ’ t match, or 1 if it did match. ( preg_match()  only finds
                         the first match within the string. If you need to find all the matches in a string, use   preg_match_all() ,
                         described later in the chapter.)

                           For example, to match the word  “ world ”  anywhere in the string  “ Hello, world! ”  you can write:


                             echo preg_match( “/world/”, “Hello, world!” );  // Displays “1”
                           To match  “ world ”  only at the start of the string, you ’ d write:

                             echo preg_match( “/^world/”, “Hello, world!” );  // Displays “0”
                           To access the text that was matched, pass an array variable as the third argument:

                             echo preg_match( “/world/”, “Hello, world!”, $match ) . “ < br / > ”;  //
                             Displays “1”
                             echo $match[0] . “ < br / > ”; // Displays “world”

                           To find out the position of the match, pass  PREG_OFFSET_CAPTURE  as the fourth argument. The array
                          then contains a nested array whose first element is the matched text and whose second element is the
                          position:
                             echo preg_match( “/world/”, “Hello, world!”,
                               $match, PREG_OFFSET_CAPTURE ) . “ < br / > ”;  // Displays “1”
                             echo $match[0][0] . “ < br / > ”;                // Displays “world”
                             echo $match[0][1] . “ < br / > ”;                // Displays “7”




                                                                                                         541





                                                                                                      9/21/09   6:17:50 PM
          c18.indd   541
          c18.indd   541                                                                              9/21/09   6:17:50 PM
   574   575   576   577   578   579   580   581   582   583   584