同事寫了一個html文本轉純文本的方法:
+ (NSString *)changeHTMLToString:(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;
}
之後使用發現各種html符號存在 ……
- (NSString *)converingStr:(NSString *)html
{
NSString *newStr = [html stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
newStr = [newStr stringByReplacingOccurrencesOfString:@" " withString:@" "];
newStr = [newStr stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
newStr = [newStr stringByReplacingOccurrencesOfString:@">" withString:@">"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"°" withString:@"°"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"•" withString:@"?"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"´" withString:@"´"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"”" withString:@"”"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"“" withString:@"“"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"‰" withString:@"‰"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"←" withString:@"←"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"↑" withString:@"↑"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"→" withString:@"→"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"↓" withString:@"↓"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"↔" withString:@"?"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"√" withString:@"√"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"∝" withString:@"∝"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"∞" withString:@"∞"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"∠" withString:@"∠"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"∨" withString:@"∨"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"∧" withString:@"∧"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"∩" withString:@"∩"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"∪" withString:@"∪"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"Ø" withString:@"Ø"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"∫" withString:@"∫"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"∴" withString:@"∴"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"≈" withString:@"≈"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"¥" withString:@"¥"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"≠" withString:@"≠"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"≡" withString:@"≡"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"≤" withString:@"≤"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"≥" withString:@"≥"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"⊕" withString:@"⊕"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"λ" withString:@"λ"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"μ" withString:@"μ"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"ν" withString:@"ν"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"ξ" withString:@"ξ"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"∏" withString:@"∏"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"∑" withString:@"∑"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"…" withString:@"…"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"¹" withString:@"¹"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"²" withString:@"²"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"³" withString:@"³"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"±" withString:@"±"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"×" withString:@"×"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"÷" withString:@"÷"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"©" withString:@""];
newStr = [newStr stringByReplacingOccurrencesOfString:@"®" withString:@"©"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"™" withString:@"®"];
newStr = [newStr stringByReplacingOccurrencesOfString:@"—" withString:@"—"];
return newStr;
}
保留存檔 替換html符號 跪求更好的轉化方法解決這個問題