Differences

This shows you the differences between two versions of the page.

Link to this comparison view

computers:php_censor_message [2015/03/24 05:42] (current)
Line 1: Line 1:
 +====== Censor Message ======
 +Description
  
 +====== Code 1 ======
 +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.
 +
 +Reference: [[http://​www.zend.com/​code/​codex.php?​id=1063&​single=1]]
 +
 +
 +
 +
 +
 +==== Usage Example ====
 +<code php> ​
 +//Just copy and paste code in a file.php on php enabled server, and call it from a browser.
 +
 +</​code>​
 +
 +==== Server Side Code  ====
 +
 +<code php>
 +<?​php ​
 + /​*********************************************** ​
 +  * Snippet Name : censorMsg ​                   * 
 +  * Scripted By  : Hermawan Haryanto ​           * 
 +  * Website ​     : http://​hermawan.dmonster.com * 
 +  * Email        : hermawan@dmonster.com ​       * 
 +  * License ​     : GPL (General Public License) * 
 +  ***********************************************/  ​
 +  $bw = "​bad|words|seperated|by|pipe|symbols";​ //these words will be replaced by "​*"​ - see lines 19.& 37.
 +  ​
 +  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"​ target="​_self"> ​
 +<​textarea name="​msg"​ cols=50 rows=10></​textarea><​br> ​
 +<input type="​submit"​ value="​Censor It"> ​
 +</​form>  ​
 +
 +</​code>​
 +
 +
 +
 +
 +====== Candidate Code Suggestions ======
 +Add your suggestions and links here.