您的位置:首页 > 移动开发 > IOS开发

iOS 过滤html标签

2015-08-25 22:26 323 查看
在项目的开发中,有时候需要加载一些html代码,html代码往往会带一些标签,但是我们不需要显示这些多余的标签,那么我们就要想办法把它给去掉。比如下面的一点html代码:

<p class="MsoNormal"style="margin-left:42.0pt;text-indent:-24.0pt;"><span style="font-size:14px;line-height:1.5;"><span style="font-size:9px;"><span style="line-height:1;"></span></span><br /></span></p><p class="MsoNormal"style="margin-left:42.0pt;text-indent:-24.0pt;"><span style="font-size:14px;line-height:1.5;">1、 经认定的软件生产企业的工资和培训费用,可按实际发生额在计算应纳税所得额时扣除。</span></p><p class="MsoNormal"style="margin-left:42.0pt;text-indent:-24.0pt;"><span style="font-size:14px;line-height:1.5;"><span style="font-size:9px;"><span style="line-height:1;"></span></span><br /></span></p><p class="MsoNormal"style="margin-left:42.0pt;text-indent:-24.0pt;"><span style="font-size:14px;line-height:1.5;">2、 对我国境内新办软件生产企业经认定后,自开始获利年度起,第一年和第二年免征企业所得税,第三年至第五年减半征收企业所得税。</span></p>

我们细心的观察,不难发现,其实我们只要把<>中的标签去掉,就可以得到我们想要的正文了。
下面我们可以用一个专门扫描字符串的类来进行处理,请看代码:

//过滤html标签
- (NSString *)removeHTML:(NSString *)html {

NSScanner *theScanner;

NSString *text = nil;
NSString *str = 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:@""];
}
return html;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: