Search code examples
htmliosobjective-cnsstringstrip

NSString without HTML markup


I have a JSON Data, and I would like to make this JSON Data into NSString without HTML markup.

Here is my JSON Data :

<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'>

I would like to get this kind of 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.

I've already test with stripHtml, but after using it, my NSString is just : 13th March

Here is my code (the text of the report is always before #gallery-1 so it's not that the problem... but with some html markup I think... (because with different markup it works!) :

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);

Solution

  • You can match the HTML tags by using the regular expression: </?[^>]*>. Using stringByReplactingMatchesInString with the regex and an empty-string replacement, you can strip the tags out.

    @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