Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert NSString HTML markup to plain text NSString?

Been searching the net for an example of how to convert HTML string markup into Plain text.

I get my information from a feed which contains HTML, I then display this information in a Text View. does the UITextView have a property to convert HTML or do I have to do it in code. I tried:

NSString *str = [NSString stringWithCString:self.fullText encoding:NSUTF8StringEndcoding];

but doesn't seem to work. Anyone got any ideas?

like image 962
Frames84 Avatar asked Apr 09 '10 09:04

Frames84


2 Answers

You can do it by parsing the html by using NSScanner class

- (NSString *)flattenHTML:(NSString *)html {

    NSScanner *theScanner;
    NSString *text = nil;
    theScanner = [NSScanner scannerWithString:html];

    while ([theScanner isAtEnd] == NO) {

        [theScanner scanUpToString:@"<" intoString:NULL] ; 

        [theScanner scanUpToString:@">" intoString:&text] ;

        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@""];
    }
    //
    html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    return html;
}

Hope this helps.

like image 135
Madhup Singh Yadav Avatar answered Nov 15 '22 18:11

Madhup Singh Yadav


If you are using UIWebView then it will be easier to parse HTML to text:

fullArticle = [webView stringByEvaluatingJavaScriptFromString:@"document.body.getElementsByTagName('article')[0].innerText;"]; // extract the contents by tag

fullArticle = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerText"]; // extract text inside body part of HTML
like image 20
Veera Raj Avatar answered Nov 15 '22 17:11

Veera Raj