Remove HTML Tags from an NSString on the iPhone
This NSString
category uses the NSXMLParser
to accurately remove any HTML
tags from an NSString
. This is a single .m
and .h
file that can be included into your project easily.
https://gist.github.com/leighmcculloch/1202238
You then strip html
by doing the following:
Import the header:
#import "NSString_stripHtml.h"
And then call stripHtml:
NSString* mystring = @"<b>Hello</b> World!!";
NSString* stripped = [mystring stripHtml];
// stripped will be = Hello World!!
This also works with malformed HTML
that technically isn't XML
.
A quick and "dirty" (removes everything between < and >) solution, works with iOS >= 3.2:
-(NSString *) stringByStrippingHTML {
NSRange r;
NSString *s = [[self copy] autorelease];
while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
s = [s stringByReplacingCharactersInRange:r withString:@""];
return s;
}
I have this declared as a category os NSString.