去掉HTML标签的几种方法
NSString *html = @"<span style=\"letter-spacing: 0.4px;\">啦啦啦,德玛西亚~</span><span style=\"letter-spacing: 0.4px;\">一点寒芒先到随后枪出如龙</span>";
NSString *str1 = [self filterHTML:html];
NSString *str2 = [self getZZwithString:html];
NSString *str3 = [self praseHtmlStr:html];
NSLog(@"");
-(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:@""];
}
return html;
}
-(NSString *)getZZwithString:(NSString *)string{
NSRegularExpression *regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|\n"
options:0
error:nil];
string=[regularExpretion stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];
return string;
}
- (NSString *)praseHtmlStr:(NSString *)htmlStr {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
return attributedString.string;
}