iOS 去掉HTML標籤
阿新 • • 發佈:2019-01-08
// 去掉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;
}
- (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;
}