Page 582 - Beginning PHP 5.3
P. 582
Part III: Using PHP in Practice
Matching Types of Characters using Character Classes
Rather than searching for a literal character, often it ’ s useful to search for a certain class or type of
character. For example, you might care only that the character is a digit, or that it is one of the letters
A, B, or C.
By placing a set of characters in square brackets, you can search for a single character that matches
any one of the characters in the set. For example, the following expression matches “ a ” , “ b ”, “ c ”,
“ 1 ”, “ 2 ”, or “ 3 ” :
echo preg_match( “/[abc123]/”, “b” ); // Displays “1”
You can specify ranges of characters using the hyphen ( - ) symbol. The following example matches the
same set of characters as the previous example:
echo preg_match( “/[a-c1-3]/”, “b” ); // Displays “1”
So you can match any letter or digit using:
echo preg_match( “/[a-zA-Z0-9]/”, “H” ); // Displays “1”
To negate the sense of a character class — that is, to match a character that is not one of the characters in
the set — place a caret ( ^ ) symbol at the start of the list:
echo preg_match( “/[abc]/”, “e” ) . “ < br / > ”; // Displays “0”
echo preg_match( “/[^abc]/”, “e” ) . “ < br / > ”; // Displays “1”
You don ’ t need to escape most of the previously mentioned special characters when they ’ re inside a char-
acter class. The exceptions are the caret, which still needs to be escaped (unless you ’ re using it to negate
the class as just shown), and the backslash, which is used for specifying shorthand character classes, as
you see in a moment.
You can also use various shorthand character classes comprising a backslash followed by one of several
letters, as follows:
Character Class Meaning
\d A digit
\D Any character that isn ’ t a digit
\w A word character (letter, digit, or underscore)
\W Any character that isn ’ t a word character
\s A whitespace character (space, tab, line feed, carriage return,
or form feed)
\S Any character that isn ’ t a whitespace character
544
9/21/09 6:17:52 PM
c18.indd 544 9/21/09 6:17:52 PM
c18.indd 544

