views:

71

answers:

1

Need to filter our swear words that are inputted to the iPhone app and inserted to our database. I'd like to catch this before passing to our database.

Currently, I was using: stringByReplacingOccurrencesOfString:@"swear" withString:@"" but this seems inefficient to list 20+ words that need to be filtered. What's the best way to approach this?

Here is my complete code

NSUserDefaults *p = [NSUserDefaults standardUserDefaults];
NSString* string1 = [[p valueForKey:@"user"]          stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString* string2 = [[p valueForKey:@"pass"] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString* string3 = [[[[[[[[[[[[[[tvA.text stringByReplacingOccurrencesOfString:@"\n" withString:@" "] stringByReplacingOccurrencesOfString:@"&" withString:@"and"] stringByReplacingOccurrencesOfString:@"ç" withString:@"c"] stringByReplacingOccurrencesOfString:@"+" withString:@"plus"] stringByReplacingOccurrencesOfString:@"swear" withString:@""] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString* urlString = [NSString stringWithFormat:@"http://domain.com/qa.php?user=%@&pass=%@&id=%@&body=%@",string1,string2,[p valueForKey:@"a"],string3];
id val1 = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
A: 

To do it the way you're doing it, it would be much more sensible to keep a list of filtered strings and their replacements — you could even use an external plist file. Then you could either loop through the list, replacing as you go, or create an NSRegularExpression if you're looking for more sophisticated filtering.

Chuck
Chuck, could you please help me with the loop code, and how to implement that?
BigMike