-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouthwash.php
175 lines (139 loc) · 6.31 KB
/
mouthwash.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/*
* author: Martin Kululanga
* date : 03 Sep 2018
How to use
.setText( ' ' ) -> void function
.censor() -> void function removes all the bad worlds
.getParsedText() -> returns corrective text
*/
//public variables
$text;
$parsedText;
$badWords = array("bad","words","okay");
$correctiveSymbol = "*****";
//function that compares a character to the English alphabet
function letterIsInAlphabet($letter) {
$alphabet = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z');
$exist = false;
for ($i = 0; $i < count($alphabet); $i++) {
if ($alphabet[$i] == $letter) {
$exist = true;
}
}
return $exist;
}
//censor sentence
function censor(){
//declare that you are using the global variables
global $text;
global $parsedText;
global $badWords;
global $correctiveSymbol;
for ($i = 0; $i < count($badWords); $i++) {
$currentBadWord = $badWords[$i];
//Phase 1: Replace all bad words with exact match
$parsedText = str_replace($currentBadWord, $correctiveSymbol,$parsedText);
//Phase 2: Find Exaggerated bad words. for instance 'Bad' --> 'Baaaaadd'
$searchingIndex = 0;
while ($searchingIndex < strlen($parsedText)) {
$firstLetterOfCurrentBadWord = strtolower($currentBadWord){0};
//Get index of first letter
$indexFirstLetter = indexOf(strtolower($parsedText), " " . $firstLetterOfCurrentBadWord, $searchingIndex);
//Get index of last letter
if ($indexFirstLetter !== -1) {
$indexLastLetter1 = indexOf(strtolower($parsedText)," ", $indexFirstLetter + 1);
$indexLastLetter2 = indexOf(strtolower($parsedText),"\t", $indexFirstLetter + 1);
$indexLastLetter3 = indexOf(strtolower($parsedText),"\n", $indexFirstLetter + 1);
$indexLastLetter = strlen($parsedText) - 1;
if ($indexLastLetter1 != -1) {
$indexLastLetter = $indexLastLetter1;
} else if ($indexLastLetter2 != -1) {
$indexLastLetter = $indexLastLetter2;
} else if ($indexLastLetter3 != -1) {
$indexLastLetter = $indexLastLetter3;
}
$word_length = (($indexLastLetter)- ($indexFirstLetter + 1));
$word = substr($parsedText, $indexFirstLetter + 1, $word_length);
//following sequence of letters to uncover bad word
$indexOfCurrentWordLetter = 0;
$wordIsBad = true;
for ($j = 0; $j < strlen($word); $j++) {
//make words to compare lower case for easier comparison
$currentBadWord = strtolower($currentBadWord);
$word = strtolower($word);
//get letters
$w1 = $currentBadWord{$indexOfCurrentWordLetter};
$w2 = $word{$j};
if ($w1 != $w2) {
//if the current character is not a letter from the alphabet
$letter_is_in_the_alphabet = letterIsInAlphabet($word{$j});
if ($letter_is_in_the_alphabet == false) {
continue;
}
//look at the next letter is continuing the bad word sequence
if ($indexOfCurrentWordLetter < strlen($currentBadWord) - 1) {
if ($currentBadWord{$indexOfCurrentWordLetter + 1} == $word{$j}) {
//make loop repeat with different index
$indexOfCurrentWordLetter++;
$j--;
} else {
$wordIsBad = false;
}
} else {
$wordIsBad = false;
}
} else {
if ($j == strlen($word) - 1) {
//if the letter of the last word matches the last letter of the criteria
if ( $word{$j} != $currentBadWord{strlen($currentBadWord) - 1} ) {
$wordIsBad = false;
}
}
}
}
//replace word if it is bad
if ($wordIsBad) {
$parsedText = substr_replace($parsedText, $correctiveSymbol,($indexFirstLetter+1), $word_length);
//reset loop
$searchingIndex = -1;
}
}
//continue loop
$searchingIndex++;
echo $searchingIndex . "<br>";
}
}
}
//it sets the text to be censered
function setText($mtext){
//declare that you are using the global variables
global $text;
global $parsedText;
$text = $mtext . "";
$parsedText = $text;
}
function getParsedText(){
//declare that you are using the global variables
global $parsedText;
return $parsedText;
}
function cursedWordExists(){
return (strtolower($text) == strtolower($parsedText));
}
/*
************************************
Extra string manipulation functions
************************************
*/
function indexOf($phrase, $txt, $fromIndex){
$phrase = substr($phrase,$fromIndex,strlen($phrase));
$index = strpos(strtolower($phrase),strtolower($txt));
if ($index == ""){
return -1;
}
return $index + $fromIndex;
}
?>