1.正常的html<>标签转换
如:::<p>哈哈</p>
-(NSString *)filterHTML:(NSString *)html
{
NSScanner * scanner = [NSScannerscannerWithString:html];
NSString * text = nil;
while([scanner isAtEnd]==NO)
{
//找到标签的起始位置
[scanner scanUpToString:@"<"intoString:nil];
//找到标签的结束位置
[scannerscanUpToString:@">"intoString:&text];
//替换字符
html = [html stringByReplacingOccurrencesOfString:[NSStringstringWithFormat:@"%@>",text]withString:@""];
}
// NSString * regEx = @"<([^>]*)>";
// html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
return html;
}
2.和平常不一样的开头和结尾<p>这样的,如:
<p>外观方面:国产Jee机,与该发动机匹配的是9速手自一体变速箱。</p><p><br/></p>
这样的就要先转换成正常的<>标签
故用下面方法
#pragma mark -- 转义html中的标签
- (NSString *)HTML:(NSString *)html{
NSScanner *theScaner = [NSScanner scannerWithString:html];
NSDictionary *dict = @{@"&":@"&", @"<":@"<", @">":@">", @" ":@"", @""":@"\"", @"width":@"wid"};
while ([theScaner isAtEnd] == NO) {
for (int i = 0; i <[dict allKeys].count; i ++) {
[theScaner scanUpToString:[dict allKeys][i] intoString:NULL];
html = [html stringByReplacingOccurrencesOfString:[dict allKeys][i] withString:[dict allValues][i]];
}
}
return html;
}