【发布时间】:2011-09-04 11:01:56
【问题描述】:
我想知道从 NSString 中去除所有 HTML/Javascript 等标签的最佳方法。
我当前使用的解决方案会留下 cmets 和其他标签,删除它们的最佳方法是什么?
我知道 OF 解决方案,例如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 He 做到了,当有人投票结束时,该评论会自动添加(为了发帖者的利益)。
-
嗯,当我看到它时,关闭计数仍然为零
-
@Mark:我会的,但我看不到“关闭”链接:)
标签: iphone objective-c