我正在尝试创建一个包含侮辱性词语的数组,当有人试图提交数组中的任何单词时,他/她将收到错误。
我已经尝试了几次,但失败了!
有人可以帮帮我吗? :)
$censor_ary = array('word1', 'word2', 'word3');
foreach ($censor_ary as $censor)
{
$word = $censor;
}
if ($_POST['mesazhi'] == $word)
{
echo '<span>Përdorimi i fjalëve fyese nuk është e mirëseardhur</span>';
}
答案 0 :(得分:3)
$badWords = array(
'bad' => '***',
'badly' => '***');
strtr("This is a bad sentence", $badWords); // This is a *** sentence
您可以创建一个包含错误单词和已清理版本(或只是asterix **)的数组。然后,您可以使用strtr()
进行过滤。
答案 1 :(得分:2)
就是这样:
$words = array('word1', 'word2', 'word3', '...');
$re_words = array();
foreach($words as $word) $re_words[] = preg_quote($word, '#');
if (preg_match('#\b(' . implode('|', $re_words) . ')\b#i', $post, $word) {
// error, the $post contains the word $word[0]
}
这将检测$ words数组中列出的任何单词。
答案 2 :(得分:1)
简单,使用foreach
语句循环遍历数组,并使用preg_match
检查提交的$_POST
变量中是否包含该单词(我假设)
或类似的东西:
$arr = array('word1','word2');
foreach ($arr as $word)
{
if (preg_match("$word",$data))
{
//error here
}
}
答案 3 :(得分:0)
您可以尝试在我的网站上运行的此代码。用变量替换所有称为变量的变量。您需要一个包含咒骂的CSV文件。此代码能够区分咒骂词和包含咒骂词的无辜词,例如Scunthorpe。它还用适当数量的星号替换该单词,并识别所有常见后缀。运行可能需要一段时间,但会显着降低误报的风险。
//inport profanities csv and list suffixes
$profanities=explode(",", file_get_contents('NAME OF YOUR CSV FILE GOES HERE'));
$suffixes=array('','s','es','e','ed','ing','ted','ting','y','ty','d','head','bag','hole','wit','tard','er','ter','en','ten','est','test','able','ible','ful','full');
//get text input
$sanitize_text=$YOUR VARIABLE HERE;
//combine profanities and sufixes
foreach($profanities as $profanity)
{
foreach($suffixes as $suffix)
{
$sanitize_terms=$profanity;
$sanitize_terms.=$suffix;
$word=$sanitize_terms;
$match_count=preg_match_all('/'.$word.'/i', $YOUR VARIABLE HERE, $matches);
for($i=0; $i<$match_count; $i++)
{
$bwstr=trim($matches[0][$i]);
$sanitize_text=preg_replace('/\b'.$bwstr.'\b/', str_repeat("*", strlen($bwstr)), $sanitize_text);
}
}
}
$YOUR VARIABLE HERE=$sanitize_text;