如何使用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'
答案 0 :(得分:1)
$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';
}
答案 1 :(得分:1)
$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