Does objective c have a strip tags function?

倾然丶 夕夏残阳落幕 提交于 2020-01-10 20:11:19

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/3627374/does-objective-c-have-a-strip-tags-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!