目标c是否具有条带标签功能?

时间:2010-09-02 13:08:11

标签: html objective-c tags strip

我正在寻找一个从字符串中删除html标签的目标C函数(自定义或内置),类似于PHP的版本,可以在这里找到:

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

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:7)

这只会删除<>字符以及它们之间的所有内容,我认为这已经足够了:

- (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;
}