0

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lee Armstrong
  • 11,420
  • 15
  • 74
  • 122
  • what's the exact string you want to end up with? do you want to remove everything inside angle brackets from the whole text? – David Maymudes Aug 15 '09 at 21:30

2 Answers2

1

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);
}
Paul McCabe
  • 1,554
  • 9
  • 10
0

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
  • 4,884
  • 4
  • 37
  • 45
  • 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 Aug 16 '09 at 03:48
  • @DavidMaymudes well, it also says that NSAttributedString was introduced with iOS 4. – Cœur Dec 14 '19 at 16:36