views:

396

answers:

2

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 ???

A: 

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];
cocoafan
http://stackoverflow.com/questions/729135/why-no-nsattributedstring-on-the-iphone would appear to indicate you can't use this on the iPhone
David Maymudes
It's a pity.
cocoafan
+1  A: 

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);
}
fatalexception