Created
January 14, 2010 06:12
-
-
Save pypt/276938 to your computer and use it in GitHub Desktop.
Trim HTML tags from NSString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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