note 25457 deleted from ref.strings by maxim
| From: | maxim@php.net | Date: | Tue, 12 Nov 2002 16:26:02 +0000 |
| Subject: | note 25457 deleted from ref.strings by maxim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-39401@lists.php.net to get a copy of this message | ||
Here's a better IsAlphaNumeric() function than those listed above. It is an enhancement of
jewfish@jewfish.net's suggestion.
My example below allows you to pass $Type for allowing you to exclusively test alpha or numeric or
both. It also allows you to pass $IgnoreChar to list what characters are permissible in the test,
such as spaces, commas, quotes, etc.
function IsAlphaNumeric($TestValue,$Type=3,$IgnoreChar="") {
#Returns TRUE if $TestValue passes the AlphaNumeric test
# of $Type, or FALSE if it doesn't pass it.
#$Type allows for specifying what to test for:
# 1 = Alpha Only
# 2 = Numeric Only
# 3 = AlphaNumeric
#IgnoreChar is a string of values to ignore in the search
# such as spaces, commas, quotes, etc.
switch($Type){
case 1:
return !(preg_match("/[^a-z,A-Z$IgnoreChar]/", $TestValue));
break;
case 2:
return !(preg_match("/[^0-9$IgnoreChar]/", $TestValue));
break;
default:
return !(preg_match("/[^a-z,A-Z,0-9$IgnoreChar]/", $TestValue));
}
}