【问题标题】:check if domain name contains certain words检查域名是否包含某些单词
【发布时间】:2016-02-09 15:59:17
【问题描述】:

我正在尝试检查域名是否包含列入黑名单的单词。以下是我目前的代码

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;
} 

以上代码适用于 casino.com 但不适用于 casino123.com ,我们将不胜感激。

注意:这不是重复提到的问题只是检查 1 个单词,我在这里检查单词数组。

【问题讨论】:

  • 谁将此标记为重复?提到的问题只是检查 1 个单词,我在这里检查单词数组
  • 这里没有重复标记。
  • 从上述可能的 dup 帖子中复制的答案 stripos(json_encode($array),'mystring') !== false 和我的意见答案 str_ireplace($banned_words, "", $str) !== $str

标签: php


【解决方案1】:

条件正好相反,应该是:

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

【讨论】:

    【解决方案2】:

    我认为你的逻辑是倒退的。如果找到禁用词,您想返回true,对吗?

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

    ...因为,当您调用该函数时,您会说:

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

    【讨论】:

      猜你喜欢
      • 2020-07-06
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 2015-03-14
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      相关资源
      最近更新 更多