|
-
Apr 7th, 2002, 11:15 AM
#1
Thread Starter
Fanatic Member
Alphanumeric only
How can I make it so only alphanumeric characters can be used?
-
Apr 7th, 2002, 02:18 PM
#2
There's no specific function in the PHP manual (I recommend anyone who uses PHP to download it in PDF format).
That means you have to do it the hard way, and compare every character in the string to an array of characters you want to allow
-
Apr 7th, 2002, 02:28 PM
#3
This is the type of function you want to be creating:
PHP Code:
function alphanumeric($string) {
$alphachars[0] = "9";
$alphachars[1] = "a";
$alphachars[2] = "b";
$alphachars[3] = "c";
$alphachars[4] = "d";
$alphachars[5] = "e";
$alphachars[6] = "f";
$alphachars[7] = "g";
$alphachars[8] = "h";
$alphachars[9] = "i";
$alphachars[10] = "j";
$alphachars[11] = "k";
$alphachars[12] = "l";
$alphachars[13] = "m";
$alphachars[14] = "n";
$alphachars[15] = "o";
$alphachars[16] = "p";
$alphachars[17] = "q";
$alphachars[18] = "r";
$alphachars[19] = "s";
$alphachars[20] = "t";
$alphachars[21] = "u";
$alphachars[22] = "v";
$alphachars[23] = "w";
$alphachars[24] = "x";
$alphachars[25] = "y";
$alphachars[26] = "z";
$alphachars[27] = "0";
$alphachars[28] = "1";
$alphachars[29] = "2";
$alphachars[30] = "3";
$alphachars[31] = "4";
$alphachars[32] = "5";
$alphachars[33] = "6";
$alphachars[34] = "7";
$alphachars[35] = "8";
for ($i=0;$i<strlen($string);$i++) {
for ($j=0;$j<36;$j++) {
if(strtolower(substr($string, $i, 1)) == $alphachars[$j]) {
$charok == 1;
}
}
if (!$charok) {
return "false";
}
$charok = NULL;
}
return "true";
}
HTH
-
Apr 7th, 2002, 04:18 PM
#4
Thread Starter
Fanatic Member
Great thanks a bunch. Just wondering how you call a function on PHP?
-
Apr 8th, 2002, 05:35 AM
#5
PowerPoster
I would recommend a minor modifiction to ric's code, at the bottom take the quotes away from around the return values, e.g change
PHP Code:
return "false";
to. Then you can just do this
PHP Code:
If (alphanumeric("something groovy")) {
....
}
If you want to test for false, put an exclamation mark before "alphanumeric"
-
Apr 8th, 2002, 06:06 AM
#6
doh! I missed that? 
You just call it like chris said,
PHP Code:
if(alphanumeric($mystring)) {
echo "The phrase is alphanumeric";
} else {
echo "The phrase is not alphanumeric";
}
or if you for some reason wanted to store the outcome in a variable, you would use:
PHP Code:
$isalpha = alphanumeric($mystring);
Using your own functions, is just like using the ones built into PHP and it's dll's. eg. mysql_connect
HTH
-
Apr 8th, 2002, 08:07 AM
#7
Thread Starter
Fanatic Member
Thanks both of you
-
Apr 8th, 2002, 08:13 AM
#8
That's OK Just don't corrupt my avatar
-
Apr 8th, 2002, 08:36 AM
#9
PowerPoster
already done mine
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|