繁体   English   中英

没有HTML标记的NSString

[英]NSString without HTML markup

我有一个JSON数据,并且我希望将此JSON数据转换为NSString而无需HTML标记。

这是我的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM