views:

142

answers:

5

Hello,

I want to do something if my sentence include one of the words in this array, How to do that ?

$sentence = "I dont give a badwordtwo";

$values = array("badwordone","badwordtwo","badwordthree","badwordfour");

Thanks...

+1  A: 

one way

$sentence = "I dont give a badwordtwo";
$values = array("badwordone","badwordtwo","badwordthree","badwordfour");
$s = explode(" ",$sentence);
foreach ($s as $a=>$b){
    if (in_array($b, $values)) {
        echo "Got $b";
    }
}

output

$ php test.php
Got badwordtwo

OR

$sentence = "I dont give a badwordtwo";
$values = array("badwordone","badwordtwo","badwordthree","badwordfour");
$s = explode(" ",$sentence);
var_dump(array_intersect($s, $values));

output

$ php test.php
array(1) {
  [4]=>
  string(10) "badwordtwo"
}
ghostdog74
Sorry for making the output numbers wrong Ghost. "badwordtwo" is certainly not 4 characters long.
Chacha102
A: 

Don't you just love php.net?

Example #1 Basic str_replace() examples

<?php
// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");

// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");

// Provides: You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);

// Provides: 2
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count;
?>
Shadi Almosri
+1  A: 

I had a similar question a while back. This answer should fit you perfectly.

http://stackoverflow.com/questions/1452622/is-this-efficient-coding-for-anti-spam/1452641#1452641

<?PHP 
$banned = array('bad','words','like','these'); 

$looksLikeSpam = false; 
foreach($banned as $naughty){ 
    if (strpos($string,$naugty) !== false){ 
        $looksLikeSpam=true; 
    } 
} 

if ($looksLikeSpam){ 
   echo "You're GROSS!  Just... ew!"; 
   die(); 
} 
?>
Doug
+3  A: 

If you want to censor an array of words in some string you can use str_ireplace:

$var = "This is my phrase.";
$var = str_ireplace( array("this", "phrase"), array("****", "*****"), $var);

edit: as chacha102 notes, you only need to use the second array to vary the number of stars,

$var = str_ireplace( array("this", "phrase"), "", $var);

is equally valid. I should also note that if you use a second array, it's length must match exactly the first array, and the replacements correspond by index.

Mark E
You don't need to make an array for the second argument unless you want the numbers of stars to match the word. If you just want 4 stars, you can just pass the second parameter a string.
Chacha102
If the task is to filter offensive content, you would have to normalize your input string (as already indicated by Gordon in a comment). Remove all space and separator character, as well as all special characters, remove diacritics and transcode special characters like omicron to their base equivalents.
0xA3
Indeed, there is a pretty good SO post on ways to create good profanity filters (http://stackoverflow.com/questions/273516/how-do-you-implement-a-good-profanity-filter); here I address only a simple case.
Mark E
A: 

This is a snippet from Kohana 3. I've always found it to be a useful function. It also allows you to censor partial words (or not).

public static function censor($str, $badwords, $replacement = '#', $replace_partial_words = TRUE)
{
  foreach ((array) $badwords as $key => $badword)
  {
    $badwords[$key] = str_replace('\*', '\S*?', preg_quote((string) $badword));
  }

  $regex = '('.implode('|', $badwords).')';

  if ($replace_partial_words === FALSE)
  {
    $regex = '(?<=\b|\s|^)'.$regex.'(?=\b|\s|$)';
  }

  $regex = '!'.$regex.'!ui';

  if (strlen($replacement) == 1)
  {
    $regex .= 'e';
    return preg_replace($regex, 'str_repeat($replacement, strlen(\'$1\'))', $str);
  }

  return preg_replace($regex, $replacement, $str);
}
efritz