目标 c 有带标签功能吗? [英] Does objective c have a strip tags function?

查看:16
本文介绍了目标 c 有带标签功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个目标 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 有带标签功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆