从NSString中删除损坏的HTML标记

时间:2014-12-02 06:54:48

标签: html ios objective-c

我的网络服务正在返回HTML内容,有时HTML字符串可能包含不完整的HTML标记

e.g: "This is some broken html tag <b"

或similler,

现在我正在将它转换为NSAttributedString,因为不完整的标签会导致问题,如果我可以从NSString中删除这些不完整的HTML标签,那将会解决,有任何建议怎么办?

2 个答案:

答案 0 :(得分:2)

试试这个: 保存从Web服务获取的字符串并使用以下命令更新它:

NSString *str=@"This is some broken html tag <b";
[str stringByReplacingOccurrencesOfString:@"<b," withString:@""];

这将删除所有出现的@&#34;

答案 1 :(得分:2)

试试这段代码

- (NSString *)removeIncompleteHTMLTagInString:(NSString *)HTMLString {
    NSArray *subStringByOpenTabs = [HTMLString componentsSeparatedByString:@"<"];
    NSArray *subStringByCloseTabs = [HTMLString componentsSeparatedByString:@">"];
    if (subStringByOpenTabs.count > subStringByCloseTabs.count) {
        return [HTMLString substringToIndex:(HTMLString.length - ((NSString *)[subStringByOpenTabs lastObject]).length) -1];
    }
    else {
        return HTMLString;
    }
}

试验:

NSLog(@"%@",[self removeIncompleteHTMLTagInString:@"This is some <xx> broken html tag<b"]);

输出为:"This is some <xx> broken html tag"