faqts : Computers : Programming : Languages : PHP : Common Problems : Tips and Tricks

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

17 of 17 people (100%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How can I remove profanity from a string?
How can I censor selected words from a string?

Jun 19th, 2003 08:20
Phlip, Loren Siebert, Nathan Wallace, http://www.php.net/manual/function.str-repeat.php3


Try using this function:
<?php
  function sanitize($text){
    //Any George Carlin or FCC fans remember the whole "deadly 7"list?
    static $naughty = array('shit', 'fuck');
    while (list(,$bad) = each($naughty)){
      $text = str_replace($bad, make_string('*', strlen($bad)), $text);
    }
    return $text;
  }
?>
Note:  Sure wish there was a stri_replace...
And make_string is a PHP4 thing, so you may need to roll your own.
>>>>>>>>>>>>
I use PHP4 but don't see make_string defined anywhere. Instead, I use 
str_repeat and it does the trick. -Loren
[Phlip:]
There is a shortcut for all this:
$naughty = array("shit" => "sh*t", "fuck" => "f*ck");
$censored = strtr($uncensored, $naughty);
Note that this was added in PHP4.0