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.
1 2
3
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:
1 2
3
4 $Replace = "$@#&*!!";
5
6
7 $Mode = 2;
8
9
10
11
12
13
14
15
16 $NaughtyList = array("ickiti", "lickybistic","itichictist",
"mustervasor", "icemoss", "actowacko");
17
18
19 ?>
1 2
9
10
11 function check_badwords($Message) {
12
13 global $Mode, $Replace, $Edit;
14 include("Badword_dictionary.php");
15 $Edit = $Message;
16
17
18 if ($Mode != 0) {
19 if (is_array($NaughtyList)) {
20 $Replace = $Replace;
21
22 if ($Mode == 1) {
23 for ($i = 0; $i < count($NaughtyList); $i++) {
24
25 $Edit = eregi_replace("$NaughtyList[$i]([^a-zA-Z0-9])","$Replace\\1",$Edit);
26 }
27
28 } elseif ($Mode == 2) {
29 for ($i = 0; $i < count($NaughtyList); $i++) {
30
31 $Edit = eregi_replace("(^|[^[:alnum:]])$NaughtyList[$i]","\\1$Replace",$Edit);
32 }
33
34 } elseif ($Mode == 3) {
35 for ($i = 0; $i < count($NaughtyList); $i++) {
36
37 $Edit = eregi_replace("$NaughtyList[$i]","$Replace",$Edit);
38 }
39 }
40 }
41 }
42 return ($Edit);
43 }
44
45
46
47 function Simple_Censor_badwords($Message) {
48 global $Edit;
49 check_badwords($Message);
50 return ($Edit);
51 }
52
53
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.