We're going to take a break from math-related functions for a few weeks (yay!) and play with regular expressions. Regular expressions are more powerful and faster than old-fashioned string parsing.
Both ASP and PHP have a function for checking if something is numeric. How about a function for checking if something is alphabetical?
ASP
function isAlpha(someString)dim regExset regEx = new RegExpwith regEx.Global = true.IgnoreCase = true.Pattern = "[A-Z\s_]"end withif regEx.test(someString) thenisAlpha = trueelseisAlpha = falseend ifset regEx = nothingend function
PHP
function is_alpha($someString){return (preg_match("/[A-Z\s_]/i", $someString) > 0) ? true : false;}
The test pattern we are using above will allow letters of the alphabet, the underscore character, and whitespace characters. With a small tweak to the test pattern, we can also write a function to check if a string is alphanumeric.
ASP
function isAlphaNumeric(someString)dim regExset regEx = new RegExpwith regEx.Global = true.IgnoreCase = true.Pattern = "[\w\s.]"end withif regEx.test(someString) thenisAlphaNumeric = trueelseisAlphaNumeric = falseend ifset regEx = nothingend function
PHP
function is_alphanumeric($someString){return (preg_match("/[\w\s.]/i", $someString) > 0) ? true : false;}
The \w switch in the pattern includes the 26 letters of the alphabet plus the numbers zero through nine.
More regular expression fun next week!
