检查字符串是否包含来自数组的单词

时间:2023-02-04 19:16:49

I have a script below that detects for words in my word filter (an array), and determines whether a string is clean or not.

我下面有一个脚本,用于检测word filter(一个数组)中的单词,并确定字符串是否干净。

What I have below works well when the words are used with spacing. But ifiwritesomething without spaces, it doesn't detect.

当这些词用空格时,我下面所介绍的方法效果很好。但如果没有空格,它就无法检测。

How can I make it such that it searches the whole string instead of words? I tried removing the explode function but I got some errors...

如何让它搜索整个字符串而不是单词?我试着移除爆炸函数,但我有一些错误……

$string = 'goodmorningnoobs';
$array = array("idiot","noob");
if(0 == count(array_intersect(array_map('strtolower', explode(' ', $string)), $array))){

echo"clean";

} else {

echo "unclean";

}

Can anyone help?

谁能帮忙吗?

2 个解决方案

#1


3  

$clean = true;

foreach ( $array as $word ) {
    if ( stripos($string, $word) !== false ) {
        $clean = false;
        break;
    }
}

echo $clean ? 'clean' : 'unclean';

#2


1  

How about?

怎么样?

$hasWords = preg_match('/'. implode('|', $words) .'/', $string);

echo $hasWords ? 'unclean' : 'clean';

#1


3  

$clean = true;

foreach ( $array as $word ) {
    if ( stripos($string, $word) !== false ) {
        $clean = false;
        break;
    }
}

echo $clean ? 'clean' : 'unclean';

#2


1  

How about?

怎么样?

$hasWords = preg_match('/'. implode('|', $words) .'/', $string);

echo $hasWords ? 'unclean' : 'clean';