我正在寻找一个从字符串中删除html标签的目标C函数(自定义或内置),类似于PHP的版本,可以在这里找到:
http://php.net/manual/en/function.strip-tags.php
任何帮助将不胜感激!
答案 0 :(得分:7)
这只会删除<
和>
字符以及它们之间的所有内容,我认为这已经足够了:
- (NSString *) stripTags:(NSString *)str
{
NSMutableString *ms = [NSMutableString stringWithCapacity:[str length]];
NSScanner *scanner = [NSScanner scannerWithString:str];
[scanner setCharactersToBeSkipped:nil];
NSString *s = nil;
while (![scanner isAtEnd])
{
[scanner scanUpToString:@"<" intoString:&s];
if (s != nil)
[ms appendString:s];
[scanner scanUpToString:@">" intoString:NULL];
if (![scanner isAtEnd])
[scanner setScanLocation:[scanner scanLocation]+1];
s = nil;
}
return ms;
}