在逻辑过滤中使用PHP stripos()

在逻辑过滤中使用PHP stripos(),php,Php,如何使用stripos过滤掉自身存在的不需要的单词。 如何扭曲下面的代码,在语法中搜索“won”不会返回true,因为“wonderful”本身就是另一个词 $grammar = 'it is a wonderful day'; $bad_word = 'won'; $res = stripos($grammar, $bad_word,0); if($res === true){ echo 'bad word present'; }else{ echo 'no b

如何使用stripos过滤掉自身存在的不需要的单词。 如何扭曲下面的代码,在语法中搜索“
won
”不会返回true,因为“
wonderful
”本身就是另一个词

$grammar = 'it is a wonderful day';
$bad_word = 'won';

$res = stripos($grammar, $bad_word,0);

if($res === true){
       echo 'bad word present';
}else{
       echo 'no bad word'; 
}

//result  'bad word present'
使用

你试过preg_match()吗?或者在变量的开始/结束处添加空格?由于您正在查找单词
won
,因此,当包含在句子中时,它应该在单词的左侧或右侧留一个空格。谢谢您,我认为如果(preg\u match(“/\bwon\b/i”,“这是美好的一天”){echo“找到了坏词。”;}否则{echo“找不到坏词。”;}//找不到坏词
$grammar = 'it is a wonderful day';
$bad_word = 'won';
$pattern = "/ +" . $bad_word . " +/i"; 

// works with one ore more spaces around the bad word, /i means it's not case sensitive

$res = preg_match($pattern, $grammar);
// returns 1 if the pattern has been found

if($res == 1){
  echo 'bad word present';
}
else{
  echo 'no bad word'; 
}
 $grammar = 'it is a wonderful day';
 $bad_word = 'won';

        /*  \b   \b indicates a word boundary, so only the distinct won not wonderful is  searched   */

 if(preg_match("/\bwon\b/i","it is a wonderful day")){ 
    echo "bad word was found";} 
 else { 
    echo "bad word not found"; 
    } 

//result is : bad word not found