Objective c里字符串NSString 过滤HTML标签的两种方法

搜索关键词 :strip tag html NSString  html


//第一种,用NSScanner扫描,来自下面这个著名的连接,不过如今打不开鸟~ objective-c

// Source: http://rudis.net/content/2009/01/21/flatten-html-content-ie-strip-tags-cocoaobjective-c spa

- (NSString *)removeHTML:(NSString *)html { .net

    NSScanner *theScanner; component

    NSString *text = nil; orm

    

    theScanner = [NSScanner scannerWithString:html]; htm

    

    while ([theScanner isAtEnd] == NO) { ip

        // find start of tag ci

        [theScanner scanUpToString:@"<" intoString:NULL] ; rem

        

        // find end of tag

        [theScanner scanUpToString:@">" intoString:&text] ;

        

        // replace the found tag with a space

        //(you can filter multi-spaces out later if you wish)

        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@" "];

        

    }

    return html;

}


//第二种,用NSString自带的Seprated自截断方法

- (NSString *)removeHTML2:(NSString *)html{

    NSArray *components = [html componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];

    

    NSMutableArray *componentsToKeep = [NSMutableArray array];

    for (int i = 0; i < [components count]; i = i + 2) {

        [componentsToKeep addObject:[components objectAtIndex:i]];

    }

    

    NSString *plainText = [componentsToKeep componentsJoinedByString:@""];

    return plainText;

}

相关文章
相关标签/搜索