Page 1 of 1

Bad word filter

Posted: Wed May 18, 2005 9:22 pm
by Todd_Z
Best method of taking out bad words and replacing them with their first letter and the rest of the letters are changed to *'s

i.e. S***, F***, A******

<Sorry if this is going to be moved to regex, i didn't know which thread to put it in...>

Posted: Wed May 18, 2005 9:29 pm
by Skara
yeah, regex. ^_^

Lesse...

Code: Select all

$badwords = array(
  '/(happy)/ie',
  '/(rainbow)/ie',
  '/(fun)/ie',
  #etc
);
$text = preg_replace($badwords,'filter("\\1")',$text);
function filter($text) {
  $stars = '';
  for ($i=1; $i < strlen($text); $i++) {
    $stars .= '*';
  }
  return $text[0].$stars;
}
Should work. :P

Posted: Wed May 18, 2005 10:38 pm
by Todd_Z

Code: Select all

$badwords = "/(happy|rainbow|fun)/ie";
$text = preg_replace($badwords,'filter("\\1")',$text);
function filter ( $text ) {
  for ($i=1; $i < strlen($text); $i++)
    $stars .= '*';
  return $text[0].$stars;
}
Glorious, Thank you!

Posted: Thu May 19, 2005 3:44 am
by phpScott
if your looking for a list neophyte kindly gave a link to a mysql file full of them. You will have to strip the mysql commands from around it but it might be easier then writing your own list from scratch.

viewtopic.php?t=33384&highlight=word+censor

Posted: Thu May 19, 2005 2:39 pm
by Todd_Z
I don't need that extensive of a list, but thanks for the reference!

Slight adjustment - I didn't like that it was taking the middle of words out... for example.... bass... so...

Code: Select all

$badWords = "/\b(f***|s***|a******|a**|c***|d***|b****)\b/ie";