-2

I am trying to check if domain name contain blacklisted words.Below is my code so far

function teststringforbadwords($string,$banned_words) {
    foreach($banned_words as $banned_word) {
        if(stristr($string,$banned_word)){
            return false;
        }
    }
    return true;
}


$banned_words= array("casino","payday","prescription");


if (teststringforbadwords("casino123.com",$banned_words)) {
echo "banned word found";
continue;
} 

Above code works for casino.com but not casino123.com , Any help will be appreciated.

Note : this is not duplicate The question mentioned just check for 1 word , I am checking array of words here.

Vishnu
  • 2,372
  • 6
  • 36
  • 58
  • who marked this as duplicate ? The question mentioned just check for 1 word , I am checking array of words here – Vishnu Feb 09 '16 at 16:03
  • 1
    There is no mark for duplicate here. – Jay Blanchard Feb 09 '16 at 16:06
  • 3
    Possible duplicate of [php check if string contains a value in array](http://stackoverflow.com/questions/19445798/php-check-if-string-contains-a-value-in-array) – Cᴏʀʏ Feb 09 '16 at 16:09
  • copied answer from the above possible dup post, `stripos(json_encode($array),'mystring') !== false`, and my opinion answer, `str_ireplace($banned_words, "", $str) !== $str` – Andrew Feb 09 '16 at 16:22

2 Answers2

4

The condition is pretty much the opposite, should be:

foreach($banned_words as $banned_word) {
    if(stristr($string,$banned_word) !== false){
        return true;
    }
}
return false;
Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
0

I think your logic is backwards. You want to return true if the banned word is found, right?

if(stristr($string,$banned_word)){
return true;
}

...because, when you call the function you say:

if(teststringforbadwords("casino123.com",$banned_words)){
echo "banned word found";
}
Jack Albright
  • 509
  • 3
  • 11