Reputation: 57
I'm making a filter of Badword that return true if their is a bad word in a string. but what happen is that whatever the user write the code return false.
I already tried to convert the arguments of stripos()
to string (just in case) but still.
I tried preg_match()
with "/$word/i", $_POST['message']
here is my function for the checking:
function MessageBad(){
$BadWord = false;
$bannedwords = file("bannedwords");
foreach($bannedwords as $word) {
if(stripos($_POST['message'], $word) !== false){
$BadWord = true;
}
}
return $BadWord;
}
but stripos($_POST['message'], $word) !== false
always return false even when I enter only a badword from the bannedwods list...
Upvotes: 0
Views: 176
Reputation: 781130
By default, the strings returned by file()
include the newline character at the end of each line. So $word
ends with a newline, and will only match if the bad word is at the end of the line.
Use the FILE_IGNORE_NEW_LINES
flag to remove the newlines.
$bannedwords = file("bannedwords", FILE_IGNORE_NEW_LINES);
You should also break out of the loop once you find a match, there's no need to keep checking other words.
Upvotes: 4