26

这是一个聊天页面。我有一个$string = "This dude is a mothertrucker". 我有一堆坏词:$bads = array('truck', 'shot', etc). 我如何检查是否$string包含中的任何单词$bad
到目前为止,我有:

        foreach ($bads as $bad) {
        if (strpos($string,$bad) !== false) {
            //say NO!
        }
        else {
            // YES!            }
        }

除非我这样做,当用户在$bads列表中输入一个单词时,输出为 NO!其次是!所以由于某种原因,代码运行了两次。

4

11 回答 11

72
function contains($str, array $arr)
{
    foreach($arr as $a) {
        if (stripos($str,$a) !== false) return true;
    }
    return false;
}
于 2012-12-10T06:06:22.193 回答
13

1)最简单的方法:

if ( in_array( 'three',  ['one', 'three', 'seven'] ))
...

2)另一种方式(同时检查另一个数组的数组):

$keywords=array('one','two','three');
$targets=array('eleven','six','two');
foreach ( $targets as $string ) 
{
  foreach ( $keywords as $keyword ) 
  {
    if ( strpos( $string, $keyword ) !== FALSE )
     { echo "The word appeared !!" }
  }
}
于 2013-05-30T12:46:00.673 回答
8

你能试试这个而不是你的代码吗

$string = "This dude is a mothertrucker";
$bads = array('truck', 'shot');
foreach($bads as $bad) {
    $place = strpos($string, $bad);
    if (!empty($place)) {
        echo 'Bad word';
        exit;
    } else {
        echo "Good";
    }
}
于 2012-12-10T07:11:34.953 回答
4

您可以翻转您的坏词数组并更快地进行相同的检查。将每个坏词定义为数组的一个键。例如,

//define global variable that is available to too part of php script
//you don't want to redefine the array each time you call the function
//as a work around you may write a class if you don't want global variable
$GLOBALS['bad_words']= array('truck' => true, 'shot' => true);

function containsBadWord($str){
    //get rid of extra white spaces at the end and beginning of the string
    $str= trim($str);
    //replace multiple white spaces next to each other with single space.
    //So we don't have problem when we use explode on the string(we dont want empty elements in the array)
    $str= preg_replace('/\s+/', ' ', $str);

    $word_list= explode(" ", $str);
    foreach($word_list as $word){
        if( isset($GLOBALS['bad_words'][$word]) ){
            return true;
        }
    }
    return false;
}

$string = "This dude is a mothertrucker";

if ( !containsBadWord($string) ){
    //doesn't contain bad word
}
else{
    //contains bad word
}

在这段代码中,我们只是检查是否存在索引,而不是将坏词与坏词列表中的所有单词进行比较。
isset 比 in_array 快得多,并且比 array_key_exists 快一点。
确保坏字数组中的任何值都没有设置为空。
如果数组索引设置为 null,isset 将返回 false。

于 2013-12-03T16:57:06.753 回答
3

一旦它发现任何不好的词,就放置并退出或死亡,就像这样

foreach ($bads as $bad) {
 if (strpos($string,$bad) !== false) {
        //say NO!
 }
 else {
        echo YES;
        die(); or exit;            
  }
}
于 2012-12-10T06:07:14.580 回答
3

有一个非常短的 php 脚本,您可以使用它来识别使用 str_ireplace 的字符串中的坏词,如下所示:

$string = "This dude is a mean mothertrucker";
$badwords = array('truck', 'shot', 'ass');
$banstring = ($string != str_ireplace($badwords,"XX",$string))? true: false;
if ($banstring) {
   echo 'Bad words found';
} else {
    echo 'No bad words in the string';
}

单行:

$banstring = ($string != str_ireplace($badwords,"XX",$string))? true: false;

做所有的工作。

于 2017-07-21T11:29:09.280 回答
2

您也可以通过这种方式进行过滤

$string = "This dude is a mothertrucker";
if (preg_match_all('#\b(truck|shot|etc)\b#', $string )) //add all bad words here.       
    {
  echo "There is a bad word in the string";
    } 
else {
    echo "There is no bad word in the string";
   }
于 2019-06-27T12:56:55.890 回答
1

想要这个?

$string = "This dude is a mothertrucker"; 
$bads = array('truck', 'shot', 'mothertrucker');

    foreach ($bads as $bad) {
        if (strstr($string,$bad) !== false) {
            echo 'NO<br>';
        }
        else {
            echo 'YES<br>';
        }
    }
于 2012-12-10T06:11:55.253 回答
0

如果聊天字符串不是那么长,我会那样做。

$badwords = array('mothertrucker', 'ash', 'whole');
$chatstr = 'This dude is a mothertrucker';
$chatstrArr = explode(' ',$chatstr);
$badwordfound = false;
foreach ($chatstrArr as $k => $v) {
    if (in_array($v,$badwords)) {$badwordfound = true; break;}
    foreach($badwords as $kb => $vb) {
        if (strstr($v, $kb)) $badwordfound = true;
        break;
    }
}
if ($badwordfound) { echo 'You\'re nasty!';}
else echo 'GoodGuy!';
于 2012-12-10T06:08:06.543 回答
0

如果您想使用 array_intersect(),请使用以下代码:

function checkString(array $arr, $str) {

  $str = preg_replace( array('/[^ \w]+/', '/\s+/'), ' ', strtolower($str) ); // Remove Special Characters and extra spaces -or- convert to LowerCase

  $matchedString = array_intersect( explode(' ', $str), $arr);

  if ( count($matchedString) > 0 ) {
    return true;
  }
  return false;
}
于 2017-08-17T06:28:18.047 回答
-2
 $string = "This dude is a good man";   
 $bad = array('truck','shot','etc'); 
 $flag='0';         
 foreach($bad as $word){        
    if(in_array($word,$string))        
    {       
        $flag=1;       
    }       
}       
if($flag==1)
  echo "Exist";
else
  echo "Not Exist";
于 2015-02-11T09:15:28.600 回答