Skip to content

Instantly share code, notes, and snippets.

@pypt
Created January 14, 2010 06:12
Show Gist options
  • Save pypt/276938 to your computer and use it in GitHub Desktop.
Save pypt/276938 to your computer and use it in GitHub Desktop.
Trim HTML tags from NSString
- (NSString *)flattenHTML:(NSString *)html
trimWhiteSpace:(BOOL)trim {
NSScanner *theScanner = [NSScanner scannerWithString:html];
NSString *text = nil;
while (! [theScanner isAtEnd]) {
// Start of the tag
[theScanner scanUpToString:@"<"
intoString:NULL];
// End of the tag
[theScanner scanUpToString:@">"
intoString:&text];
// Replace the found tag with a space
html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text]
withString:@" "];
}
// Trim off whitespace
return trim ? [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] : html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment