Reputation: 11450
I have the following string...
Overall: 21 (1,192,742<img src="/images/image/move_up.gif" title="+7195865" alt="Up" />)<br />
August: 21 (1,192,742<img src="/images/image/move_up.gif" title="+722865" alt="Up" />)<br />
I need to remove the HTML tag, is there a way I can say remove everything between <img
and />
?
Upvotes: 0
Views: 958
Reputation: 1554
Are you wishing to remove all of the HTML content from your string? If so, you could approach it in the following manner:
- (void)removeHtml:(NSString *) yourString
{
NSString *identifiedHtml = nil;
//Create a new scanner object using your string to parse
NSScanner *scanner = [NSScanner scannerWithString: yourString];
while (NO == [scanner isAtEnd])
{
// find opening html tag
[scanner scanUpToString: @"<" intoString:NULL] ;
// find closing html tag - store html tag in identifiedHtml variable
[scanner scanUpToString: @">" intoString: &identifiedHtml] ;
// use identifiedHtml variable to search and replace with a space
NSString yourString =
[yourString stringByReplacingOccurrencesOfString:
[ NSString stringWithFormat: @"%@>", identifiedHtml]
withString: @" "];
}
//Log your html-less string
NSLog(@"%@", yourString);
}
Upvotes: 1
Reputation: 4894
I'm not sure if this will work on iPhone (because initWithHTML:documentAttributes: is an AppKit addition) but I've tested it for a Cocoa app
NSString *text = "your posted html string here";
NSData *data = [text dataUsingEncoding: NSUnicodeStringEncoding];
NSAttributedString *str =
[[[NSAttributedString alloc] initWithHTML: data documentAttributes: nil] autorelease];
NSString *strippedString = [str string];
Upvotes: 0