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;
}
<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;
}
相关文章推荐
- iOS中比较日期
- iOS自定义警告框 OC 如何写可变参数的函数
- 如何用iOS Simulator模拟键盘上的return键去除键盘
- ios开发遇到的memory持续上涨导致页面crash解决思路总结
- iOS学习(1)
- IOS启动动流程
- iOS使用极光推动的实现
- ios中正确的裁剪图片,这样不必要每次都向服务器下载图片。
- iOS下OpenGL ES 3.0编程入门(二 ):画一个简单三角形
- xcode7免证书真机调试
- iOS笔记
- IOS上传文件开发
- 学习IOS中
- iOS 时间戳转换为时间
- iOS 宏(define)与常量(const)的正确使用
- 【读书笔记】iOS-读取文本文件
- iOS中Block介绍(一)基础
- 【读书笔记】iOS-读取文本文件
- ios内联函数 inline
- iOS之弹幕效果