5.8 Checking Characters
TechniqueUse eregi () in conjunction with character classes: <?php if (eregi ("^[a-z]+$", $line)) { // .. do if true } else { // .. do if false } ?> Or use the predefined character class along with ereg() : <?php if (ereg ("^[[:alpha:]]+$", $line)) { // .. do if true } else { // .. do if false } ?> CommentsIn the first solution, we use eregi() , which performs a case-insensitive query using a specified pattern (in this case, [a-z] ). As discussed in the introduction, the regular expression [a-z] is a character class that will match any lowercase letter from a to z. Because we are using a case-insensitive search, [a-z] will match any character. We put a plus after the [a-z] character class to indicate that there must be one or more occurrences. The entire regular expression is enclosed within ^ and $ , meaning that the entire string must match the pattern (this is also discussed in the introduction). In the second solution, we simply substitute eregi() (case-insensitive) with ereg() (case-sensitive), and instead of [a-z] we use [[:alpha:]] which is a predefined character set that matches any alphabetic character. PHP provides several of these predefined character classes, listed in the following table.
These are just easy ways to access commonly used character classes. To negate reserved classes, make sure that you put the ^ inside the first bracket : [^[:space:]] // Match anything that isn't a space |