php中if语句的另一种方法是使用过多的“;或者“||”


Alternative way in php for if statement with too many "OR" ||

我有一个if语句,它在type=text 的输入中检查这些"冒犯性单词"

if(strstr($key,'bi**h') || strstr($key,'fu**k') || strstr($key,'son of a   bit**') || strstr($key,'cun*') || strstr($key,'fuc**r') || strstr($key,'mother fuc**') || strstr($key,'shi**') || strstr($key,'cr**p') ){
      $erros['insult']='please avoid any offending words';
       }

这段代码是由另一位开发人员编写的,我只是想知道除了为我必须输入的每个单词添加双管之外,还有其他方法吗?。我想,这看起来是一种原始的编写代码的方式。

小心坏词过滤器,单词shitake怎么办?这里有一个简单的方法来替换坏单词并对照原始字符串进行检查:

$bad = array('bi**h', 'fu**k', 'son of a   bit**', 'cun*', 'fuc**r', 'mother fuc**', 'shi**', 'cr**p');
if(str_replace($bad, '', $key) != $key) {
      $erros['insult']='please avoid any offending words';
}

然而,if||方法将更快。

我猜您要么使用带有or的strstr/strpos,要么使用正则表达式。如果您有一个可枚举的停止词列表,我建议您避免使用regexp。