Search code examples
htmlobjective-ctagsstrip

Does objective c have a strip tags function?


I am looking for an objective C function (custom or built-in) that strips html tags from a string, similar to PHP's version that can be found here:

http://php.net/manual/en/function.strip-tags.php

Any help would be appreciated!


Solution

  • This just removes < and > characters and everything between them, which I suppose is sufficient:

    - (NSString *) stripTags:(NSString *)str
    {
        NSMutableString *ms = [NSMutableString stringWithCapacity:[str length]];
    
        NSScanner *scanner = [NSScanner scannerWithString:str];
        [scanner setCharactersToBeSkipped:nil];
        NSString *s = nil;
        while (![scanner isAtEnd])
        {
            [scanner scanUpToString:@"<" intoString:&s];
            if (s != nil)
                [ms appendString:s];
            [scanner scanUpToString:@">" intoString:NULL];
            if (![scanner isAtEnd])
                [scanner setScanLocation:[scanner scanLocation]+1];
            s = nil;
        }
    
        return ms;
    }