我正在寻找一个目标 C 函数(自定义或内置),它可以从字符串中去除 html 标记,类似于可以在此处找到的 PHP 版本:
I am looking for an objective C function (custom or built-in) that strips html tags from a string, similar to PHP's version that can be found here:
http://php.net/manual/en/function.strip-tags.php
任何帮助将不胜感激!
这只是删除 <
和 >
字符以及它们之间的所有内容,我认为这已经足够了:
This just removes <
and >
characters and everything between them, which I suppose is sufficient:
- (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;
}
这篇关于目标 c 有带标签功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!