How can I make it so only alphanumeric characters can be used?
Printable View
How can I make it so only alphanumeric characters can be used?
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 :(
This is the type of function you want to be creating:
HTH :)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";
}
Great thanks a bunch. Just wondering how you call a function on PHP?
I would recommend a minor modifiction to ric's code, at the bottom take the quotes away from around the return values, e.g changetoPHP Code:return "false";
. Then you can just do thisPHP Code:return false;
If you want to test for false, put an exclamation mark before "alphanumeric"PHP Code:If (alphanumeric("something groovy")) {
....
}
doh! I missed that? :rolleyes:
You just call it like chris said,
or if you for some reason wanted to store the outcome in a variable, you would use:PHP Code:if(alphanumeric($mystring)) {
echo "The phrase is alphanumeric";
} else {
echo "The phrase is not alphanumeric";
}
Using your own functions, is just like using the ones built into PHP and it's dll's. eg. mysql_connectPHP Code:$isalpha = alphanumeric($mystring);
HTH :)
Thanks both of you :)
That's OK :) Just don't corrupt my avatar :D
already done mine :( ;)