[iphone-objective C]去掉一段String中的HTML标签

本来想找一个如何能把HTML的String,解析出来的工具。暂时还没有找到。但是找到一段很不错的code。

 

可以移调里面的标签。 



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

} 

posted @ 2010-05-21 23:17  AlexLiu  阅读(1726)  评论(4编辑  收藏  举报