Bill
80.***.***.***Verifying that a string contains only authorized characters - simple function
This is a simple function I wrote which will return FALSE if the supplied string ($a) contains one or more characters not in the array of authorized characters, and TRUE otherwise. It can be used for such things as enforcing character policies on usernames or passwords, and a myriad of other things.PHP code:
function legalchars($a){
$b=array(" ","'","-","a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X","y","Y","z","Z","0","1","2","3","4","5","6","7","8","9");
$c=strlen($a);
for($d=0;$d<$c;$d++){
if(!in_array($a[$d],$b)){
return FALSE;
}
}
return TRUE;
}
It's use should be self-explanatory. Simply reference the string you want to check with legalchars() and act upon in if it returns FALSE.
To customize the authorized characters, simply edit the array named $b. Remember that the in_array() check is case sensitive, so if you want to allow the character B you probably will want to supply both the upper- and lower case versions.