Page 587 - Beginning PHP 5.3
P. 587

Chapter 18: String Matching with Regular Expressions
                             echo preg_match( “/^\d+\:/”, “12: The Sting” );           // Displays “1”
                             echo preg_match( “/^\d+\:/”, “Die Hard 2: Die Harder” );  // Displays “0”

                           The caret ( ^ ) symbol specifies that the rest of the pattern will only match at the start of the string.
                         Similarly, you can use the dollar (  $ ) symbol to anchor a pattern to the end of the string:

                             echo preg_match( “/\[(G|PG|PG-13|R|NC-17)\]$/”, “The Sting [PG]” ); //
                             Displays “1”
                             echo preg_match( “/\[(G|PG|PG-13|R|NC-17)\]$/”, “[PG] Amadeus” );   //
                             Displays “0”

                           By combining the two anchors, you can ensure that a string contains only the desired pattern,
                         nothing more:

                             echo preg_match( “/^Hello, \w+$/”, “Hello, world” );  // Displays “1”

                             echo preg_match( “/^Hello, \w+$/”, “Hello, world!” ); // Displays “0”
                           The second match fails because the target string contains a non - word character ( ! ) between the searched -
                            for pattern and the end of the string.

                            You can use other anchors for more control over your matching. Here ’ s a full list of the anchors you can
                          use within a regular expression:



                              Anchor         Meaning
                               ^              Matches at the start of the string
                               $              Matches at the end of the string

                               \b             Matches at a word boundary (between a  \w  character and a  \W  character)
                               \B             Matches except at a word boundary
                               \A             Matches at the start of the string

                               \z             Matches at the end of the string
                               \Z             Matches at the end of the string  or  just before a newline at the end of the string
                               \G             Matches at the starting offset character position, as passed to the  preg_match()
                                          function

                               It ’ s important to note that an anchor doesn ’ t itself match any characters; it merely ensures that the
                               pattern appears at a specified point in the target string.











                                                                                                         549





          c18.indd   549                                                                              9/21/09   6:17:54 PM
          c18.indd   549
                                                                                                      9/21/09   6:17:54 PM
   582   583   584   585   586   587   588   589   590   591   592