Possible duplicate:
Remove HTML tags from NSString on iPhone
I would like to know the best method for removing all HTML / Javascript tags from NSString.
The current solution I'm using leaves comments and other tags, what would be the best way to remove them?
I know the solutions, for example. LibXML, but I would like some examples to work with.
Current solution:
- (NSString *)flattenHTML:(NSString *)html trimWhiteSpace:(BOOL)trim {
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString:html];
while ([theScanner isAtEnd] == NO) {
[theScanner scanUpToString:@"<" intoString:NULL] ;
[theScanner scanUpToString:@">" intoString:&text] ;
html = [html stringByReplacingOccurrencesOfString:
[ NSString stringWithFormat:@"%@>", text]
withString:@""];
}
return trim ? [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] : html;
}
source
share