Author Topic: [PHP] Word Censor Script  (Read 104 times)

DeptNav

  • Administrator
  • Newbie
  • *****
  • Posts: 36
    • View Profile
[PHP] Word Censor Script
« on: January 15, 2014, 08:26:23 am »
Code: [Select]
<?php
function censor$str )
{
    
/*
    load bad words, separated by line break, eg:
 
    <a href="https://www.createaforum.com/censorinfo.php?subdomain=deptnav" target="_blank">****</a>
    <a href="https://www.createaforum.com/censorinfo.php?subdomain=deptnav" target="_blank">****</a>
    <a href="https://www.createaforum.com/censorinfo.php?subdomain=deptnav" target="_blank">****</a>
    */
    
$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_wordtrim$str ) );
            
$new_word '';
        }
    }
return 
$str;
}
 
$string "Hello you <a href="https://www.createaforum.com/censorinfo.php?subdomain=deptnav" target="_blank">****</a> <a href="https://www.createaforum.com/censorinfo.php?subdomain=deptnav" target="_blank">****</a>!";
 
echo $string//Hello you <a href="https://www.createaforum.com/censorinfo.php?subdomain=deptnav" target="_blank">****</a> <a href="https://www.createaforum.com/censorinfo.php?subdomain=deptnav" target="_blank">****</a>!
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.

Share on Facebook Share on Twitter