Zend - The PHP Company




Forms

Add Code


censormsg  

Type: application
Added by: hermawan
Entered: 03/11/2002
Last modified: 01/11/2001
Rating: - (fewer than 3 votes)
Views: 6345
This is a simple function which can be used when you have a form for your users to post their message and you want to censor every badwords that they might write on that form.


<?php 
 
/*********************************************** 
  * Snippet Name : censorMsg                    * 
  * Scripted By  : Hermawan Haryanto            * 
  * Website      : http://hermawan.dmonster.com * 
  * Email        : hermawan@dmonster.com        * 
  * License      : GPL (General Public License) * 
  ***********************************************/  
  
$bw "words|seperated|by|pipe|symbols"
  function 
str_repeats($input$mult) { 
    
$ret ""
    while(
$mult>0) { 
      
$ret .= $input
      
$mult --; 
    } 
    return 
$ret
  } 
  function 
censorMsg($msg$replacement="*") { 
    global 
$bw
    
$badwords explode("|"$bw); 
    
$eachword explode(" "$msg); 
    for(
$j=0;$j<count($badwords);$j++) { 
      for(
$i=0;$i<count($eachword);$i++) { 
        if(
is_int(strpos(strtolower($eachword[$i]), $badwords[$j]))) { 
          
$msg eregi_replace($eachword[$i], 
                               
str_repeats($replacement
                                           
strlen($eachword[$i])), 
                               
stripslashes($msg)); 
        } 
      } 
    } 
    return 
$msg
  } 
  
$msg $_POST["msg"]; 
  if (isset(
$msg)) { 
    
$msg censorMsg($msg,"*"); 
    print 
"Your posted Message :<br>".$msg
  } 
?> 
<form method="post"> 
<textarea name="msg" cols=50 rows=10></textarea><br> 
<input type="submit" value="Censor It"> 
</form> 


Usage Example


See the example


Rate This Script





Search



This Category All Categories