Bir dizi bir dizi sözcük bir dizi konulmuşsa belirlemek

5 Cevap

Ben bir dize süzülmüş kelime tespit ederse bir script öldürecek basit bir kelime filtresi gerekir.

benim sözlerim aşağıdaki gibidir söylüyor

$showstopper = array(badword1, badword2, badword3, badword4);

$yourmouth = "im gonna badword3 you up";

if(something($yourmouth, $showstopper)){ 
//stop the show
}

5 Cevap

Eğer düzenli ifadeye badwords dizisi implode ve samanlıkta karşı eşleşen olmadığını görebiliyordu. Yoksa tek tek her kelime sadece dizi boyunca döngü ve kontrol edebilir.

From the comments:

$re = "/(" . implode("|", $showstopper) . ")/"; //  '/(badword1|badword2)/'
if (preg_match($re, $yourmouth) > 0) { die("foulmouth"); }

Sen kriter istiyorum bu foreach ve preg_match vs yaklaşımlar olabilir.

$showstopper = array('badword1', 'badword2', 'badword3', 'badword4');
$yourmouth = "im gonna badword3 you up";

$check = str_replace($showstopper, '****', $yourmouth, $count);
if($count > 0) { 
     //stop the show
}

in_array () senin arkadaşın

    $yourmouth_array = explode(' ',$yourmouth);
    foreach($yourmouth_array as $key=>$w){
       if (in_array($w,$showstopper){
         // stop the show, like, replace that element with '***'
         $yourmouth_array[$key]= '***';
       }
    }
$yourmouth = implode(' ',$yourmouth_array);

Hızlı bir çözüm bu dizinin üzerinde yineleme gerekmez gibi anahtarını denetleyerek içerir. Ancak, kötü kelimeler listesi bir değişiklik gerektirecektir.

$showstopper = array('badword1' => 1, 'badword2' => 1, 'badword3' => 1, 'badword4' => 1);
$yourmouth = "im gonna badword3 you up";

// split words on space
$words = explode(' ', $yourmouth);
foreach($words as $word) {
    // filter extraneous characters out of the word
    $word = preg_replace('/[^A-Za-z0-9]*/', '', $word);
    // check for bad word match
    if (isset($showstopper[$word])) {
        die('game over');
    }
}

preg_replace Kullanıcıların * bad_word3 * gibi bir şey yazarak filtre kötüye yok sağlar. Ayrıca dizi anahtarı onay bomba değil sağlar.

Bunu ancak heres kullanılan kötü kelimeleri kontrol ve almak için bir yol gerekir neden emin değil

$showstopper = array(badword1, badword2, badword3, badword4);
$yourmouth = "im gonna badword3 you up badword1";

function badWordCheck( $var ) {

    global $yourmouth;
    if (strpos($yourmouth, $var)) {
    	return true;
    }

}

print_r(array_filter($showstopper, 'badWordCheck'));

array_filter () kötü kelime bir dizi döndürür, böylece bunun sayısı () 0 nothign kötü ise denildi