1

This is my code:

NSURL *url=[NSURL URLWithString:@"http://www.engadget.com"];
NSString *webPage=[[NSString alloc]initWithContentsOfURL:url
                          encoding:NSUTF8StringEncoding error:nil];

In the webPage string I got an html page of the link. In that string there is lot of tags and text. I want to take only body of the text without any tags.

I want to display that text into my UITextView. How can I do that?

Flexo
  • 87,323
  • 22
  • 191
  • 272
  • 1
    Is this what you are looking for? [Remove HTML Tags From an NSString on the iPhone](http://stackoverflow.com/questions/277055/remove-html-tags-from-an-nsstring-on-the-iphone) – Nick Stamas Jun 05 '09 at 14:53

3 Answers3

1

This is the best answer and is exactly what you are looking for:

Write the following script in the webView delegate method. (UIWebviewdidfinishLoading)

NSString *myText = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];
Flexo
  • 87,323
  • 22
  • 191
  • 272
Biranchi
  • 16,120
  • 23
  • 124
  • 161
0

From what I tried, this did the job best. Even though the NSSCanner is not the smaerter solution for this, if the html/xml is well formed you should be fine.

Dimitris
  • 13,480
  • 17
  • 74
  • 94
0

Better Solution:

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

    NSScanner *theScanner;
    NSString *text = nil;

    theScanner = [NSScanner scannerWithString:html];

    while ([theScanner isAtEnd] == NO) {

        // find start of tag
        [theScanner scanUpToString:@"<" intoString:NULL] ; 

        // find end of tag
        [theScanner scanUpToString:@">" intoString:&text] ;

        // replace the found tag with a space
        //(you can filter multi-spaces out later if you wish)
        html = [html stringByReplacingOccurrencesOfString:
                           [ NSString stringWithFormat:@"%@>", text]
                     withString:@" "];

    } // while //

    return html;

}

Reference: http://rudis.net/content/2009/01/21/flatten-html-content-ie-strip-tags-cocoaobjective-c

Flexo
  • 87,323
  • 22
  • 191
  • 272
Jimit
  • 652
  • 8
  • 12
  • 1
    This would appear to be a direct copy and paste from this blog post: http://rudis.net/content/2009/01/21/flatten-html-content-ie-strip-tags-cocoaobjective-c You should provide attribution when copying someone else's code. – Nick Forge Aug 24 '10 at 10:09
  • my bad. I was in train when I wrote it. – Jimit Oct 18 '10 at 20:36