html - 没有 HTML 标记的 NSString

标签 html ios objective-c nsstring strip

我有一个 JSON 数据,我想在没有 HTML 标记的情况下将这个 JSON 数据变成 NSString。

这是我的 JSON 数据:

<p><strong style="font-size: 13px;">13th March</strong></p>
<p><span id="fbPhotoSnowliftCaption" class="fbPhotosPhotoCaption" tabindex="0" data-ft="{"tn":"K"}"><span class="hasCaption">Peak is solid 6ft + a bit wild but not a breath of wind around. Rossnowlagh will be the pick of the beaches. Very good surf forecast all weekend and into the middle of next week</span></span>.</p>
<p><span id="more-113"></span></p>
<p>High tide: 10:56, 3.2m    <span style="color: #ff0000;"> <a href="http://www.bundoransurfco.com/webcam/"><strong></strong></a></span></p>
<p>Low Tide: 16:32, 1.4m</p>
<p><b>3 day forecast to March 16th</b></p>
<p>Windy midweek but the surf is looking very good from Friday onward, right through the weekend.</p>
        <style type='text/css'>

我想获得这种 NSString :

13th March
Peak is solid 6ft + a bit wild but not a breath of wind around. Rossnowlagh will be the pick of the beaches. Very good surf forecast all weekend and into the middle of next week.
High tide: 10:56, 3.2m
Low Tide: 16:32, 1.4m
3 day forecast to March 16th
Windy midweek but the surf is looking very good from Friday onward, right through the weekend.

我已经用 stripHtml 进行了测试,但在使用它之后,我的 NSString 只是:13th March

这是我的代码(报告的文本始终位于 #gallery-1 之前,所以这不是问题......但我认为有一些 html 标记......(因为使用不同的标记有效!):

NSString* stringBDD = [[dicoBDD objectForKey:@"post"] objectForKey:@"content"];
NSString *stringWithoutLiveWebcam = [stringBDD stringByReplacingOccurrencesOfString:@"CLICK HERE FOR LIVE PEAK WEBCAM" withString:@""];
NSString *stringReportFirstPart = [[stringWithoutLiveWebcam componentsSeparatedByString:@"#gallery-1"] firstObject];
NSString* stringWithoutHtml = [stringReportFirstPart stripHtml];
NSLog(@"stringWithoutHtml : %@", stringWithoutHtml);

最佳答案

您可以使用正则表达式来匹配 HTML 标签:</?[^>]*> 。使用 stringByReplactingMatchesInString使用正则表达式和空字符串替换,您可以删除标签。

@interface NSString (HTML)
- (NSString *)stripTags;
@end

@implementation NSString (HTML)
- (NSString *)stripTags {
  NSError *error = nil;
  NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"</?[^>]*>"
                                                                         options:0
                                                                           error:&error];
  if (error == nil) {
    return [regex stringByReplacingMatchesInString:self
                                           options:0
                                             range:NSMakeRange(0, self.length)
                                      withTemplate:@""];
  } else {
    return self;
  }
}
@end

关于html - 没有 HTML 标记的 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29037399/

相关文章:

html - 阻止 MS Edge 上的输入建议

html - 可见浏览器回流

objective-c - 更改 MKMapKit MKAnnotation 子类中的图像

ios - 旋转添加到 keyWindow 的 UIView

ios - segue 后在 View 上保留标签栏?

objective-c - IBOutlet已连接但不工作

javascript - HTML 样式 ="overflow:hidden"使用 JavaScript/jQuery 访问内部溢出的元素?

java - 如何使用 <s :iterator>? 从单个集合中绘制多个 HTML 表

ios - Facebook IOS SDK - 如何知道何时返回应用程序

iphone - 显示当前周的开始和结束日期