I have created an Android app wherein the users can give reviews and comments the products. Is there a way to control users from writing bad words in the reviews and comments or is there any sdk available for doing that.
Asked
Active
Viewed 971 times
2 Answers
1
Do it server side if you're using php and If you're just trying to do a simple word filter, create a single long regexp with all of the banned phrases that you want to censor, and merely do a regex find/replace with it. A regex like:
$filterRegex = "(boogers|snot|poop|shucks|argh|fudgecicles)"
and run it on your input string using preg_match() to wholesale test for a hit,
or preg_replace() to blank them out.

Rohit Suthar
- 3,528
- 1
- 42
- 48

M4HdYaR
- 1,124
- 11
- 27
-
That's how my _butterfly_ website got banned by IT department. – Cœur Jul 19 '18 at 04:59
1
You have to create a class for the censor module, this is somewhat greedy in implementation.
public class WordFilter {
static String[] words = {"bad", "words"};
public static String censor(String input) {
StringBuilder s = new StringBuilder(input);
for (int i = 0; i < input.length(); i++) {
for (String word : words) {
try {
if (input.substring(i, word.length()+i).equalsIgnoreCase(word)) {
for (int j = i; j < i + word.length(); j++) {
s.setCharAt(j, '*');
}
}
} catch (Exception e) {
}
}
}
return s.toString();
}
public static void main(String[] args) {
System.out.println(censor("String with bad words"));
}
}

Jon Church
- 2,320
- 13
- 23

Joshua Dapitan
- 9
- 4
-
1The issue only is that this implementation is prone to clbuttic mistakes. – Joshua Dapitan Mar 22 '16 at 12:50