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



这是针对聊天页面的。 我有一个$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!,后跟 YES!,因此由于某种原因,代码运行了两次。

function contains($str, array $arr)
{
    foreach($arr as $a) {
        if (stripos($str,$a) !== false) return true;
    }
    return false;
}

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 !!" }
  }
}

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

$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";
    }
}

有一个非常短的 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;

完成所有工作。

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

//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。
如果数组索引设置为 null,则 ISSET 将返回 false。

一旦发现任何坏词就放并退出或死亡,就像这样

foreach ($bads as $bad) {
 if (strpos($string,$bad) !== false) {
        //say NO!
 }
 else {
        echo YES;
        die(); or exit;            
  }
}
您也可以

通过这种方式进行过滤

$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";
   }

想要这个吗?

$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>';
        }
    }
如果你想

用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;
}

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

$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!';
 $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";

相关内容

  • 没有找到相关文章

最新更新