可能重複:
Remove HTML Tags from an NSString on the iPhone地帶HTML標籤等從NSString的
我想知道剝離掉所有的HTML/JavaScript廣告等標記出一個NSString的最佳方法。
目前的解決方案,我用樹葉意見等標籤,這將是刪除它們的最好方法?
我知道解決方案,例如作者LibXML,但我希望有一些例子可以使用。
目前的解決方案:
- (NSString *)flattenHTML:(NSString *)html trimWhiteSpace:(BOOL)trim {
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:@""];
}
// trim off whitespace
return trim ? [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] : html;
}
@ x3ro中投票關閉爲重複 – Mark 2011-05-29 21:33:42
@馬克,他做到了,這是評論自動添加(爲了海報的利益)當一個人投票結束時。 – benzado 2011-05-29 21:35:53
嗯收盤數仍然爲零,當我看到它 – Mark 2011-05-29 21:36:54