remove banned words from a string

Started on the old forum in June 2010 · view the original in the archive · old links redirect here

6 replies · 1,367 views · started 16 years ago · 1 min read

7 participants
GumSlone Administrator Principal · 1,690 posts #1

i have many spammers at my sites so i have developed a function which removes site names or just swear words from a string.

Here is a function:

function gum_banned_words($str,$words)
{
	$found_words = gum_banned_words_check($str,$words);
	//Developed by gumslone from coding-talk.com
	foreach ($found_words as $word) {
		$word_length = strlen($word);
		$str_length = strlen($str);
		$i = 0;
		$n = 0;
		while($i<$word_length)
		{
			if (preg_match('/[0-9A-Za-z]/',$str[$n]))
			{
				$find = "/".$word[$i]."/i";
				if (preg_match($find, $str[$n]))
				{
					if($i==0) $result['start'] = $n;
					if($i==($word_length-1))
					{
						$result['end'] = $n+1;
						break;
					}
					$i++;
				}
				else 
				{
					$i=0;
				}
			}
			$n++;
			if($n>$str_length)break;//prevent infinite loop
		}
		$str = substr($str, 0, $result['start']).substr($str, $result['end']);
	}
	$found_word2 = gum_banned_words_check($str,$words);
	if(is_array($found_word2)) $str = gum_banned_words($str,$found_word2);
	
	return $str;
}
function gum_banned_words_check($str, $words)
{
	$clean_string = preg_replace('/[^[:alnum:]]/', '', $str);
	foreach ($words as $word) {
		$find = "/".$word."/i";
		if (preg_match($find, $clean_string)) $found_words[] = $word;
	}
	return $found_words;
}

usage example:

$words = array('admob','inmobi','http');
$text = 'admob sucks http://admob.com A:d:-mOb, and i n m o :b --i';

echo gum_banned_words($text,$words);

output will be:

sucks ://.com , and

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

djlee Developer · 224 posts #2

v.nice, had to do something similar last year on a project, i never thought of using a callback to iterate over the resultset though. I ended up using some over complicated regex's which i dont know how it even worked to be honest (thank god for expresso).

If i remember correctly (i could be wrong) there is a few php functions around (or could be a few flat file db's, it been a while) which has a translation table for things like "hacker text" and "sms text" which takes your badword and returns its translated equivalent(s). I used it once upon a time on a torrent site so as not have to type the hundreds of possible combinations kids can think of to say the word f@@k. May be a useful plugin to this function, i'll have to have a look around when i get the time and see if i can dig up the info for it

something else Fellow · 2,296 posts #3

nice function gum 🙂 i tried coding something similar a while ago for a friend but i kept returning something like: suckscomand

nice share 🙂

jaidon20k Developer · 64 posts #5

help me

how am i going to use this function on my site, did i need to put this $word=array('wap','http','web','net'); $text='admob sucks http://anysite.com A:d:- and i n m o :b-i'; echo gum_banned_words($text,$words); to my inbxpro.php?action=sendpm

youngbobby Developer · 55 posts #6

jaidon20k wrote: how am i going to use this function on my site, did i need to put this $word=array('wap','http','web','net'); $text='admob sucks http://anysite.com A:d:- and i n m o :b-i'; echo gum_banned_words($text,$words); to my inbxpro.php?action=sendpm

Re-read the post

Naijatop.net = Mobile Traffic Booster 👑

📖 YoungBobby.com = Bobby's BLoG 😁

You're Welcome !!

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close