日期:2014-05-17 浏览次数:20578 次
最近在用phonegap,经常出现字符串带有html标签。下面有个方法一步到位去掉HTML标签
+(NSString *)flattenHTML:(NSString *)html trimWhiteSpace:(BOOL)trim
{
NSScanner *theScanner = [NSScanner scannerWithString:html];
NSString *text = nil;
while ([theScanner isAtEnd] == NO) {
// find start of tag
[theScanner scanUpToString:@"<" intoString:NULL] ;
// find end of tag
[theScanner scanUpToString:@">" intoString:&text] ;
// replace the found tag with a space
//(you can filter multi-spaces out later if you wish)
html = [html stringByReplacingOccurrencesOfString:
[ NSString stringWithFormat:@"%@>", text]
withString:@""];
}
return trim ? [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] : html;
}
? 调用方法:
? ??notification33.alertBody =[self flattenHTML:body trimWhiteSpace:YES];