I am trying to create an array that include insulting words and when someone tries to submit any of the words that are inside the array, he/she will get an error.
I've tried a couple of time, but failed!
Can anyone help me out please? :)
$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>';
}
$badWords = array(
'bad' => '***',
'badly' => '***');
strtr("This is a bad sentence", $badWords); // This is a *** sentence
You can create an array which contains bad words and cleaned versions (or just asterix **) . And then, you can use strtr()
for filtering.
Here is it:
$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]
}
This will detect any word listed in the $words array.
Simple, loop through the array with foreach
statement, and use preg_match
to check if the word is included in the submitted $_POST
variable (I am assuming)
Or something like this:
$arr = array('word1','word2');
foreach ($arr as $word)
{
if (preg_match("$word",$data))
{
//error here
}
}
You can try this code that has worked on my website. Replace all variables called your variable here with your variable. You need a CSV file that contains the expletives. This code is able to distinguish between a swear word and an inocent word containing a swear word, for example Scunthorpe. It also replaces the word with the appropriate number of stars, and will recognise all common suffixes. It can take a while to run, but significantly reduces the risk of false positives.
//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;
精彩评论