Last active
December 29, 2015 15:29
-
-
Save sag333ar/7690524 to your computer and use it in GitHub Desktop.
Decode or strip HTML tags from a string
This file contains hidden or 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
// sample data is as follows | |
//// <h1>some data</h1> <h2>some other data</h2> | |
// and if you wish to extract data as follows | |
//// some data some other data | |
// use following class for decoding HTML string to normal text | |
@interface STEntitiesConverter : NSObject <NSXMLParserDelegate> | |
@property (nonatomic, retain) NSMutableString* resultString; | |
- (NSString*)convertEntiesInString:(NSString*)s; | |
@end | |
@implementation STEntitiesConverter | |
- (id)init { | |
if([super init]) { | |
self.resultString = [NSMutableString string]; | |
} | |
return self; | |
} | |
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)s { | |
[self.resultString appendString:s]; | |
} | |
- (NSString*)convertEntiesInString:(NSString*)s { | |
if(s == nil) { | |
NSLog(@"ERROR : Parameter string is nil"); | |
} | |
NSString* xmlStr = [NSString stringWithFormat:@"<d>%@</d>", s]; | |
NSData *data = [xmlStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; | |
NSXMLParser* xmlParse = [[NSXMLParser alloc] initWithData:data]; | |
[xmlParse setDelegate:self]; | |
[xmlParse parse]; | |
return [NSString stringWithFormat:@"%@",self.resultString]; | |
} | |
- (void)dealloc { | |
self.resultString = nil; | |
} | |
/// ----------- | |
// sample usage | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
NSString *str = @"<p>Intel Core</p>"; | |
STEntitiesConverter *converter = [[STEntitiesConverter alloc] init]; | |
NSString *str2 = [converter convertEntiesInString:str]; | |
NSLog(@"Stripped string is %@",str2); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment