从NSString中删除HTML标签等 [英] Strip out HTML Tags etc from NSString
本文介绍了从NSString中删除HTML标签等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道从NSString中删除所有HTML / Javascript等标签的最佳方法。
I would like to know the best method for stripping out all HTML/Javascript etc tags out of an NSString.
我正在使用的当前解决方案留下评论和其他标签,删除它们的最佳方法是什么?
The current solution I am using leaves comments and other tags in, what would be the best way to remove them?
我知道解决方案,例如LibXML,但我想要一些例子。
I know OF solutions e.g. LibXML, but I would like some examples to work with.
当前解决方案:
- (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;
}
推荐答案
尝试使用此方法删除HTML来自字符串的标签:
Try this method to remove HTML tags from a String:
- (NSString *)stripTags:(NSString *)str
{
NSMutableString *html = [NSMutableString stringWithCapacity:[str length]];
NSScanner *scanner = [NSScanner scannerWithString:str];
scanner.charactersToBeSkipped = NULL;
NSString *tempText = nil;
while (![scanner isAtEnd])
{
[scanner scanUpToString:@"<" intoString:&tempText];
if (tempText != nil)
[html appendString:tempText];
[scanner scanUpToString:@">" intoString:NULL];
if (![scanner isAtEnd])
[scanner setScanLocation:[scanner scanLocation] + 1];
tempText = nil;
}
return html;
}
这篇关于从NSString中删除HTML标签等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文