U.S. Department of the Navy

General Category => U.S. Fleet Cyber Command => Topic started by: DeptNav on January 15, 2014, 08:26:23 am


Title: [PHP] Word Censor Script
Post by: DeptNav on January 15, 2014, 08:26:23 am
Code: [Select]
<?phpfunction censor( $str ){    /*    load bad words, separated by line break, eg:     ****    ****    ****    */    $bad_words = file( "bad_words.txt" );     foreach ( $bad_words as $bad_word )    {        $bad_word = trim( $bad_word );        if ( preg_match( "/" . $bad_word . "/i", $str ) )        {            $new_word = '';            for ( $i = 1; $i <= strlen( $bad_word ); $i++ )            {                $new_word .= '*';            }            $str = eregi_replace( $bad_word, $new_word, trim( $str ) );            $new_word = '';        }    }return $str;} $string = "Hello you **** ****!"; echo $string; //Hello you **** ****!echo censor( $string ); //Hello you ****ing *****!?>


Code: [Select]
/*
load bad words, separated by line break, eg:

****
****
****
*/
$bad_words = file( "bad_words.txt" );
If you'd like, you can edit which bad words you'd like to censor ^. As you progress down the code, you'll come across:


Code: [Select]
$bad_word = trim( $bad_word );
if ( preg_match( "/" . $bad_word . "/i", $str ) )
{
$new_word = '';
for ( $i = 1; $i <= strlen( $bad_word ); $i++ )
{
$new_word .= '*';
Feel free to alter the filter from '*' to anything such as 'bobba' or '%@!*&amp;amp;' etc.