简体   繁体   English

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

[英]Using PHP stripos() in Logical Filtering

How can i use the stripos to filter out unwanted word existing on itself. 我如何使用Stripos过滤掉自身上存在的不需要的单词。 How do i twist the code below that a search for ' won ' in grammar will not return true, since ' wonderful ' is another word itself. 我如何扭曲之下,一个关于“代码won语法不会回到真正的”,因为“ 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'

Use preg_match 使用preg_match

$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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM