Page 1 of 1
Checking for specific words in a variable
Posted: Tue Jul 01, 2003 3:49 am
by Drachlen
While i was testing my 'create account' page, i noticed it was allowing me to type in periods and spaces as the username. The code im using is:
Code: Select all
<?php
function is_username_valid($username) {
if(eregi("^[a-z0-9]",$username)) return TRUE;
else return FALSE;
}
?>
And i dont see any periods or spaces in it, how do i identify them?... Also, i was wondering how to use some function to find specific letters next to eachother in a variable, ie profanity. Id like to make sure people cant sign up with an offensive or racist name.
Posted: Tue Jul 01, 2003 4:03 am
by qartis
Personally, I hate regular expressions. I'd just go with
Code: Select all
if (!stristr($username," ") && !stristr($username,".") && !stristr($username,"fsck") && !stristr($username,"asp") && !stristr($username,"a$$")){
return true;
} else {
return false;
}
Posted: Tue Jul 01, 2003 4:03 am
by twigletmac
It's probably because your regular expression is just matching an alphnumeric character at the beginning of the username and then returning true. Try:
Code: Select all
function is_username_valid($username)
{
return (preg_match('/[^a-z0-9]/i', $username)) ? TRUE : FALSE;
}
Mac
Posted: Tue Jul 01, 2003 4:07 am
by qartis
He said he wants them together, so..
Code: Select all
if (eregi("^[a-z0-9]",$username) && !stristr($username," ") && !stristr($username,".") && !stristr($username,"fsck") && !stristr($username,"asp") && !stristr($username,"a$$")){
return true;
}
is how you'd do it.
EDIT: Although if you just want to make sure all the characters are letters, you might as well just do
Code: Select all
$username = trim($username);
if (!is_string($username)){
return false;
}
$badword = array("asp","bla","foo","bar");
foreach ($badword as $cuss){
if (stristr($username,$cuss)){
return false;
}
}
return true;
Posted: Tue Jul 01, 2003 5:17 am
by twigletmac
is_string() won't check for the non-alphanumeric characters but you can still do this with no regular expressions using
ctype_alnum().
Mac