Bad word filter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Bad word filter

Post 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...>
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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!
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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";
Post Reply