我想做一些事情,如果我的句子包括這個數組中的單詞之一,該怎麼做?如果句子包含此數組中的單詞之一,該怎麼做?
$sentence = "I dont give a badwordtwo";
$values = array("badwordone","badwordtwo","badwordthree","badwordfour");
謝謝...
我想做一些事情,如果我的句子包括這個數組中的單詞之一,該怎麼做?如果句子包含此數組中的單詞之一,該怎麼做?
$sentence = "I dont give a badwordtwo";
$values = array("badwordone","badwordtwo","badwordthree","badwordfour");
謝謝...
如果要審查的話數組中一些字符串就可以使用str_ireplace
:
$var = "This is my phrase.";
$var = str_ireplace(array("this", "phrase"), array("****", "*****"), $var);
編輯:作爲chacha102筆記,你只需要使用第二陣列以改變星星的數量,
$var = str_ireplace(array("this", "phrase"), "", $var);
同樣是VA蓋。我還應該注意,如果你使用第二個數組,它的長度必須與第一個數組完全匹配,並且替換符合索引。
除非要讓恆星數與單詞相匹配,否則不需要爲第二個參數創建數組。如果你只是想要4顆星,你可以將第二個參數傳遞給一個字符串。 – 2010-01-06 02:24:07
如果任務是過濾令人討厭的內容,則必須對輸入字符串進行規範化(正如Gordon在評論中已經指出的那樣)。刪除所有空格和分隔符,以及所有特殊字符,刪除變音符號並將特殊字符(如omicron)轉換爲其基本等效字符。 – 2010-01-06 11:20:31
事實上,在創建良好的褻瀆過濾器的方法上有一個相當不錯的帖子(http://stackoverflow.com/questions/273516/how-do-you-implement-a-good-profanity-filter);這裏我只談到一個簡單的例子。 – 2010-01-06 12:31:33
單程
$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";
}
}
輸出
$ 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));
輸出
$ php test.php
array(1) {
[4]=>
string(10) "badwordtwo"
}
對不起,使輸出數字錯誤的幽靈。 「badwordtwo」當然不是4個字符長。 – 2010-01-06 02:26:14
難道你不喜歡php.net?
實例#1基本str_replace()函數的例子
<?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;
?>
我也有類似的問題,而回。這個答案完全適合你。
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();
}
?>
這是Kohana 3的一個片段。我總是發現它是一個有用的功能。它也允許你檢查部分單詞(或不單單)。
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);
}
您可能會考慮爲您的示例選擇不同的單詞。 – 2010-01-06 02:17:22
@Mark:我想他可能會試圖從用戶字符串中篩選出不好的單詞.. [但我同意你] – 2010-01-06 02:18:35
Sanitized :) ...我做了我自己的'壞話' – 2010-01-06 02:19:33