How to do something if sentence include one of the words in this array?
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");
Than开发者_如何学Cks...
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.
I had a similar question a while back. This answer should fit you perfectly.
Is this efficient coding for anti-spam?
<?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();
}
?>
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"
}
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;
?>
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);
}
精彩评论