Forum Moderators: coopster
/* This will strip $anystring of everything but A-Z,a-z,0-9 to create $newstring */$anystring="Whatever you want it to be! --!@#$%^&*()";
$newstring="";
for ($i = 0; $i < strlen($anystring); $i++) {
/* step through the string... a case select() would be better... */
if ((ord($anystring[$i]) > 47) && (ord($anystring[$i]) < 58))
$newstring .= $anystring[$i]; // 0-9 [48-57]
if ((ord($anystring[$i]) > 64) && (ord($anystring[$i]) < 91))
$newstring .= $anystring[$i]; // A-Z [65-90]
if ((ord($anystring[$i]) > 96) && (ord($anystring[$i]) < 123))
$newstring .= $anystring[$i]; // a-z [97-122]
}
echo $newstring;
/* both strings will be the same size if the original string was OK, so... */
$isAllAlphaNumeric=FALSE;
if (strlen($newstring) == strlen($anystring))
$isAllAlphaNumeric=TRUE;
A function from the PHP man pages that allows some punctuation...
function cleanstr($string){
$len = strlen($string);
for($a=0; $a<$len; $a++){
$p = ord($string[$a]);
# chr(32) is space, it is preserved..
(($p > 64 && $p < 123) ¦¦ $p == 32)? $ret .= $string[$a] : $ret .= "";
}
return $ret;
}
[edited by: spinnercee at 12:40 am (utc) on Sep. 10, 2006]
example:
if(ctype_alnum($string)){
echo "string contains only a-z A-Z 0-9";
}
else{
echo "string doesn't contain only a-z A-Z 0-9";
}
Andrew