Page 581 - Beginning PHP 5.3
P. 581

Chapter 18: String Matching with Regular Expressions
                             echo preg_match( “/http\:\/\//”, “http://www.example.com” );  // Displays “1”

                           Slashes are commonly used as regular expression delimiters, but you can use any symbol you like
                         (provided you use the same symbol at both the start and end of the expression). This is useful if your
                         expression contains a lot of slashes. By using a different delimiter, such as the   |  (vertical bar) character,
                         you avoid having to escape the slashes within the expression:


                             echo preg_match( “|http\://|”, “http://www.example.com” );  // Displays “1”
                           Although some of these special characters are sometimes treated literally in certain contexts, it ’ s a good
                         idea always to escape them. (However, don ’ t try to escape letters and digits by placing a backslash in
                         front of them, because this conveys a different special meaning, as you see later.)

                           Luckily, PHP provides a handy function called   preg_quote()  that takes a string and returns the same
                          string with any special regular expression characters quoted:


                             echo preg_quote( “$3.99” );  // Displays “\$3\.99”
                           If you want to escape your delimiter character also, pass it as the second argument to  preg_quote() :

                             echo preg_quote( “http://”, “/” );  // Displays “http\:\/\/”
                            preg_quote()  is particularly useful for inserting strings into your regular expression at run - time
                         (because you can ’ t tell in advance whether the string contains any special characters that need escaping).

                           You can also write various characters literally within regular expressions by using escape sequences,
                         as follows:



                              Escape Sequence        Meaning
                               \n                  A line feed character (ASCII 10)

                               \r                  A carriage return character (ASCII 13)
                               \t                  A horizontal tab character (ASCII 9)
                               \e                An escape character (ASCII 27)

                               \f                  A form feed character (ASCII 12)
                               \a                  A bell (alarm) character (ASCII 7)



                               \x dd               A character with the hex code dd (for example,  \x61  is the ASCII letter  “ a ” )


                               \ ddd               A character with the octal code ddd (for example,  \141  is the ASCII letter  “ a ” )
                               \c                  A control character (for example,  \cH  denotes  ^H , or backspace)
                             x





                                                                                                         543





          c18.indd   543                                                                              9/21/09   6:17:51 PM
          c18.indd   543
                                                                                                      9/21/09   6:17:51 PM
   576   577   578   579   580   581   582   583   584   585   586