Show DevBest [PHP] Word Censor

Status
Not open for further replies.

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
I wasn't sure whether this was a tutorial or a free script, but I can't be bothered to explain the code properly so I'm just going to dump the script - there for it is a free script.

So anyway, I coded this for my college website I am currently working on and thought people may be interested in it.

PHP:
<?php
function censor( $str )
{
    /*
    load bad words, separated by line break, eg:
    
    shit
    fuck
    bitch
    */
    $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 fucking bitch!";
 
echo $string; //Hello you fucking bitch!
echo censor( $string ); //Hello you ****ing *****!
?>
 

brsy

nah mang
May 12, 2011
1,530
272
I wasn't sure whether this was a tutorial or a free script, but I can't be bothered to explain the code properly so I'm just going to dump the script - there for it is a free script.

So anyway, I coded this for my college website I am currently working on and thought people may be interested in it.

PHP:
<?php
function censor( $str )
{
    /*
    load bad words, separated by line break, eg:
   
    shit
    fuck
    bitch
    */
    $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 fucking bitch!";
 
echo $string; //Hello you fucking bitch!
echo censor( $string ); //Hello you ****ing *****!
?>
Mind explaining the foreach and the preg_match functions?
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
Mind explaining the foreach and the preg_match functions?
If you look at the line '$bad_words = file( "bad_words.txt" );' - the file() function automatically splits each line in the bad_words.txt and put them into an array, so the foreach() function loops through each of the bad words. The preg_match() function performs an expression on a string based on a pattern you give it. In this case, I'm more-or-less asking preg_match() "is one of the bad words in $str?", if the statement is true, it will then do another loop and found out how many letters are in the bad word, then it will replace each letter with a *.

That is as best I can put it.

... and thanks Spotz :)
 

brsy

nah mang
May 12, 2011
1,530
272
If you look at the line '$bad_words = file( "bad_words.txt" );' - the file() function automatically splits each line in the bad_words.txt and put them into an array, so the foreach() function loops through each of the bad words. The preg_match() function performs an expression on a string based on a pattern you give it. In this case, I'm more-or-less asking preg_match() "is one of the bad words in $str?", if the statement is true, it will then do another loop and found out how many letters are in the bad word, then it will replace each letter with a *.

That is as best I can put it.

... and thanks Spotz :)
Thanks, I really understand it now <3
 

Alam

shietttt
Jul 3, 2011
433
166
Nice work , love the way you use strings to direct to a directory / or another variable. This is quite useful.
 
Status
Not open for further replies.

Users who are viewing this thread

Top