Abhishek
Abhishek

Reputation: 1702

Remove broken HTML tags from NSString

My webservice is returning me HTML content, Sometimes that HTML string might contains incomplete HTML tags

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

or similler,

Now I am converting it to NSAttributedString during that incomplete tags are causing problems, It would be solved if I could remove these incomplete HTML tags from NSString, Any suggestions how to do it?

Upvotes: 0

Views: 154

Answers (2)

larva
larva

Reputation: 5148

try this code

- (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;
    }
}

test:

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

output is: "This is some <xx> broken html tag"

Upvotes: 2

Logic
Logic

Reputation: 705

Try this: Save string obtained from web services and update it using:

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

This will remove all the occurrences of @"

Upvotes: 2

Related Questions