美文网首页
iOS 去掉HTML标签的几种方法

iOS 去掉HTML标签的几种方法

作者: _Waiting_ | 来源:发表于2023-03-26 10:22 被阅读0次

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

相关文章

网友评论

      本文标题:iOS 去掉HTML标签的几种方法

      本文链接:https://www.haomeiwen.com/subject/iryordtx.html