1. 程式人生 > >iOS 去掉HTML標籤

iOS 去掉HTML標籤

// 去掉HTML標籤
- (NSString *)filterHTML:(NSString *)html {
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text = nil;
    while([scanner isAtEnd]==NO)
    {
        //找到標籤的起始位置
        [scanner scanUpToString:@"<" intoString:nil];
        //找到標籤的結束位置
        [scanner scanUpToString:@">" intoString:&text];
        //替換字元
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    }
    //    NSString * regEx = @"<([^>]*)>";
    //    html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
    return html;
}