I am tryiny to write code that denies any username that contains bad words. No matter what I do - I get "Invalid username."
$f = @fopen("censor.txt", "r");
$bw = fread($f, filesize("censor.txt"));
$banned_words = explode("\n", $bw);
function teststringforbadwords($wantusername, $banned_words)
{
foreach ($banned_words as $banned_word) {
if (stristr($wantusername, $banned_word)) {
return FALSE;
}
}
return TRUE;
}
if (!teststringforbadwords($wantusername, $banned_words)) {
echo 'string is clean';
} else {
echo('string contains banned words');
$message = "Invalid username.";
}
@fclose($f);
I am currently learning php and have tried everything I can think of to get it to work - help!
The function works fine, but you call it in not correct way, because the function return False
if a bad word if matched:
if( teststringforbadwords( $wantusername, $banned_words ) )
{
echo 'string is clean';
}
else
{
echo('string contains banned words');
$message = "Invalid username.";
}
Otherwise, if you want maintain coherence with function name, you have to invert True
and False
returns inside function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With