Page 127 - Open Soource Technologies 304.indd
P. 127
Unit 5: Strings
Special metacharacters in the form [:...:] can be used in character lists to match other character Notes
classes. For example, the character class specifications [:alnum:] can be used to check for
alphanumeric strings:
$str = “abc123”;
// Evaluates to true
$result = ereg(‘^[[:alnum:]]+$’, $str);
$str = “abc\xf623”;
// Evaluates to false because of the \xf6 character
$result = ereg(‘^[[:alnum:]]+$’, $str);
Be careful to use special metacharacter specifications only within a character list. Outside this
context, the regular expression evaluator treats the sequence as a list specification:
$str = “abc123”;
// Oops, left out the enclosing [] pair, Evaluates to false
$result = ereg(‘^[:alnum:]+$’, $str);
Table 5.6: Shows the POSIX Character Class Specifications Supported by PHP
Pattern Matches
[:alnum:]
Letters and digits
[:alpha:]
Letters
[:blank:]
The Space and Tab characters
[:cntrl:]
Control characters—those with an ASCII code less than 32
[:digit:]
Digits. Equivalent to \d
[:graph:]
Characters represented with a visible character
[:lower:]
Lowercase letters
[:print:]
Characters represented with a visible character, and the space and
tab characters
[:space:]
Whitespace characters. Equivalent to \s
[:upper:]
Uppercase letters
[:xdigit:]
Hexadecimal digits
LOVELY PROFESSIONAL UNIVERSITY 121