html轉純文本後 殘留符號代碼解決問題


同事寫了一個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:@"&copy;" withString:@""];
    newStr = [newStr stringByReplacingOccurrencesOfString:@"&reg;" withString:@"©"];
    newStr = [newStr stringByReplacingOccurrencesOfString:@"™" withString:@"®"];
    
    newStr = [newStr stringByReplacingOccurrencesOfString:@"—" withString:@"—"];
    
    
    return newStr;
    
}

保留存檔 替換html符號 跪求更好的轉化方法解決這個問題
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章