Objective c裏字符串NSString 過濾HTML標籤的兩種方法

搜索關鍵詞 :strip tag html NSString  html


//第一種,用NSScanner掃描,來自下面這個著名的連接,不過如今打不開鳥~ objective-c

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

- (NSString *)removeHTML:(NSString *)html { .net

    NSScanner *theScanner; component

    NSString *text = nil; orm

    

    theScanner = [NSScanner scannerWithString:html]; htm

    

    while ([theScanner isAtEnd] == NO) { ip

        // find start of tag ci

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

        

        // 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:@" "];

        

    }

    return html;

}


//第二種,用NSString自帶的Seprated自截斷方法

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

    NSArray *components = [html componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];

    

    NSMutableArray *componentsToKeep = [NSMutableArray array];

    for (int i = 0; i < [components count]; i = i + 2) {

        [componentsToKeep addObject:[components objectAtIndex:i]];

    }

    

    NSString *plainText = [componentsToKeep componentsJoinedByString:@""];

    return plainText;

}

相關文章
相關標籤/搜索