Reputation: 235
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.
Upvotes: 1
Views: 1114
Reputation: 1164
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.
Upvotes: 1
Reputation: 9
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"));
}
}
Upvotes: 1