Click here to Skip to main content
Click here to Skip to main content

Censoring Function in PHP

By , 2 Jun 2011
 

Introduction

This PHP code censores bad words. Words that are inappropriate for the greater good of the public, LOL. Therefore, replacing bad content with any desired word such as CENSORED or $@#&* is useful to alter and alert about these inappropriate text to the reader on your webpage / site. This function is useful for editing huge web content that may have certain words you wish to omit.

Anyways, this article is for beginners. All you need to do is install XAMPP (Google it ;)). Download php_filterapps.zip and unzip it to a place it in a directory called htdocs. Then open your browser and type http://localhost/php_filterapps/index.php. Then you will see what will happen. You are good to go!!

Note:

This code only filters those words in the Naughty list but you can add words to it.

Using the code

The code provided is a PHP function. Web developers will be able to plug it in into their webware or web site. Simply call the function "myfilter(string);" with the string you wish to filter.

There are two useful functions: myfilter() and check_mybadwords(). To censor a particular text, use the myfilter() function. The check_mybadwords() function will validate certain conditions.

// index.php function
// The default page or home page
//
  1    2  
  3  /*
  4     Index 
  5                       
  6     ============================================                                                       
  7     2011 by Sir.Dre'                           
  8                                        
  9  */
 10  
 11  
 12  require_once("function_main.php");
 13  
 14  
 15  function PreviewStory() {
 16      global $NaughtyList;
 17      $subject = "Topic: The Bad Guy posting on your web site!!!";
 18      $storyext = " I am a bad guy posting on your website. So I curse words like ickiti " + 
            "lickybistic itichictist mustervasor icemoss actowacko. However, " + 
            "in the function you have the option to change how you would like to Censor each text. ";
 19      
 20      
 21      $f_storyext = Simple_Censor_badwords($storyext);
 22      $subject = Complex_Censor_badwords($subject, "nohtml", 0);
 23      
 24      
 25      $story2 = "<p>-------------------------------Full Story" + 
                   "---------------------------------</p>$f_storyext"; 
 26  
 27      echo "<table border="\" cellpadding="\" cellspacing="\" bgcolor="\" width="\">"
 28      ."<tr>"
 29        ."<td height="\"><table border="\" cellpadding="\" cellspacing="\" bgcolor="\" width="\">"
 30          ."<tr>"
 31            ."<td width="\" height="\" align="\" valign="\" bgcolor="\">";
 32            echo "</td>"
 33            ."<td width="\" align="\" bgcolor="\"><font class="\" color="\">"
 34            ."<b>$subject </b></font><br />";
 35          echo "</td></tr>"
 36          ."<tr>"
 37            ."<td height="\" colspan="\" align="\" valign="\"> </td>"
 38            ."</tr>"
 39        ."</table></td>"
 40        ."</tr>"
 41      ."<tr>"
 42        ."<td height="\" valign="\" class="\"><font class="\">$story2 </font></td>"
 43        ."</tr>"
 44        ."<tr><td height="\"></td></tr>"
 45    ."</table>";
 46      echo "<br>";
 47  
 48  }
 49  
 50    
 51  switch($op) {
 52  
 53      default:
 54      PreviewStory();
 55      break;
 56  
 57  }
 58  
 59  ?>

Here is the rest of the code:

// Badword_dictionary.php function
// Config file or Checking and validating the NaughtyList
//
 
  1    2 
  3  //Replace Censored word with
  4  $Replace = "$@#&*!!";
  5 
  6  //Censored mode
  7  $Mode = 2;
  8 
  9  //Few common Jamaican badwords
 10  //$NaughtyList = array("bloodcalth", "rasscalth", "pussyhole", "bombohole", "battybwoy");
 11 
 12  //Add your country to the naughty list of badwords. lol
 13 
 14  //Build the Badword Database array
 15  //Place as much as you can.
 16  $NaughtyList = array("ickiti", "lickybistic","itichictist", 
                          "mustervasor", "icemoss", "actowacko");
 17 
 18 
 19  ?> 
// Censor_mybadwords.php function
// Config file or Checking and validating the NaughtyList
//   //
 
  1    2  /*
  3     Censor_mybadwords
  4     Apps: PHP Censor_mybadwords                     
  5     ============================================                                                      
  6     2011 by Sir.Dre'                          
  7                                    
  8  */
  9 
 10  
 11  function check_badwords($Message) {
 12     
 13      global $Mode, $Replace, $Edit;
 14      include("Badword_dictionary.php");
 15      $Edit = $Message;
 16     
 17      //Censoring Mode from 0 to 3   
 18      if ($Mode != 0) {
 19          if (is_array($NaughtyList)) {
 20              $Replace = $Replace;
 21              // Mode 1 = Exact Match
 22              if ($Mode == 1) {
 23                  for ($i = 0; $i < count($NaughtyList); $i++) {
 24                      // preg_replace doesn't work with my Apache server so i used 'eregi_replace'
 25                      $Edit = eregi_replace("$NaughtyList[$i]([^a-zA-Z0-9])","$Replace\\1",$Edit);
 26                  }
 27              //Mode 2 = Match the word at the beginning
 28              } elseif ($Mode == 2) {
 29                  for ($i = 0; $i < count($NaughtyList); $i++) {
 30                      // preg_replace doesn't work with my Apache server so i used 'eregi_replace'
 31                      $Edit = eregi_replace("(^|[^[:alnum:]])$NaughtyList[$i]","\\1$Replace",$Edit);
 32                  }
 33              //Mode 3 = Match the word anywhere in the text
 34              } elseif ($Mode == 3) {
 35                  for ($i = 0; $i < count($NaughtyList); $i++) {
 36                      // preg_replace doesn't work with my Apache server so i used 'eregi_replace'
 37                      $Edit = eregi_replace("$NaughtyList[$i]","$Replace",$Edit);
 38                  }
 39              }
 40          }
 41      }
 42      return ($Edit);
 43  }
 44 
 45 
 46  //Simple badword filter
 47  function Simple_Censor_badwords($Message) {
 48      global $Edit;
 49      check_badwords($Message);
 50      return ($Edit);
 51  }
 52 
 53  //Complex badword filter
 54  function Complex_Censor_badwords($what, $strip="", $save="") {
 55      if ($strip == "nohtml") {
 56          $what = check_badwords($what);
 57      }
 58      if ($save == 1) {
 59          $what = " -- ";
 60     
 61      } else {
 62          $what = check_badwords($what);
 63      
 64      }
 65      return($what);
 66  }
 67 
 68 
 69 
 70  ?>

Points of Interest

Some words are necessary to be censored due to the impact it may have on the public. Many cultures use different curse words that can't be filtered by others. Therefore, it's good to build a universal database. Working on it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Andre' Gardiner
Software Developer
Jamaica Jamaica
Member
Just being fascinated.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalhttp://www.codeproject.com/Tips/205603/Censoring_Function_in...memberMike Magee6 Jun '11 - 13:43 
GeneralRe: There is no option to upload codes in tips/trick section. So...memberAndre' Gardiner10 Jun '11 - 16:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 2 Jun 2011
Article Copyright 2011 by Andre' Gardiner
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid