0

This is my NSString :

 NSString timeString = @"<h5 style="direction:ltr"><span data-version-created-date="20180326T120530.000+0000" class="releasedDate">26-Mar-2018 12:05:30</span></h5>";

I want to retrieve only "26-Mar-2018 12:05:30" which is in the span tag. How do i do that in Objective C?

Please note : The given HTML is in NSString format.

iPhoneDeveloper
  • 958
  • 1
  • 14
  • 23
  • You can convert your HTMLString to `NSAttributedString`, then reading `myAttributedString.string` should give you "26-Mar-2018 12:05:30". Else, you can use `NSScanner`, `rangeOf:` and parse it. – Larme Mar 26 '18 at 13:43
  • 1
    look on this - https://stackoverflow.com/questions/277055/remove-html-tags-from-an-nsstring-on-the-iphone – Nirav Kotecha Mar 26 '18 at 14:10

4 Answers4

1

Try this

- (NSString *)stringByStrippingHTML : (NSString*) s {
    NSRange r;
    while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
    s = [s stringByReplacingCharactersInRange:r withString:@""];
    return s;
}
Lenin
  • 675
  • 6
  • 17
  • NSString *timeString = @"
    26-Mar-2018 12:05:30
    "; NSLog([self stringByStrippingHTML:timeString]);
    – Lenin Mar 26 '18 at 14:03
0

This will work by stripping out bracketed (<>) expressions. Slashes have been added () to timeString to make it proper NSString*. The stripping is repeated four times, bt should probably be looped with condition.

NSString * timeString = @"<h5 style=\"direction:ltr\"><span data-version-created-date=\"20180326T120530.000+0000\" class=\"releasedDate\">26-Mar-2018 12:05:30</span></h5>";

NSRange openRange = [timeString rangeOfString:@"<"];
NSRange closeRange = [timeString rangeOfString:@">"];
NSRange enclosedRange = NSMakeRange(openRange.location, closeRange.location-openRange.location+1);
timeString = [timeString stringByReplacingCharactersInRange:enclosedRange withString:@""];

openRange = [timeString rangeOfString:@"<"];
closeRange = [timeString rangeOfString:@">"];
enclosedRange = NSMakeRange(openRange.location, closeRange.location-openRange.location+1);
timeString = [timeString stringByReplacingCharactersInRange:enclosedRange withString:@""];

openRange = [timeString rangeOfString:@"<"];
closeRange = [timeString rangeOfString:@">"];
enclosedRange = NSMakeRange(openRange.location, closeRange.location-openRange.location+1);
timeString = [timeString stringByReplacingCharactersInRange:enclosedRange withString:@""];

openRange = [timeString rangeOfString:@"<"];
closeRange = [timeString rangeOfString:@">"];
enclosedRange = NSMakeRange(openRange.location, closeRange.location-openRange.location+1);
timeString = [timeString stringByReplacingCharactersInRange:enclosedRange withString:@""];

NSLog(@"timeString = %@", timeString);
FryAnEgg
  • 493
  • 4
  • 12
0

This worked with me

 NSString *timeString = @"<h5 style=\"direction:ltr\"><span data-version-created-date=\"20180326T120530.000+0000\" class=\"releasedDate\">26-Mar-2018 12:05:30</span></h5>";
  NSRegularExpression *regex = [NSRegularExpression
                              regularExpressionWithPattern:@">\\d.+\\d<"
                              options:NSRegularExpressionCaseInsensitive
                              error:NULL];
[regex enumerateMatchesInString:timeString options:0 range:NSMakeRange(0, [timeString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
    // your code to handle matches here

    NSString *subString = [timeString substringWithRange:match.range];

    NSLog(@"%@",[subString substringWithRange:NSMakeRange(1, subString.length - 2)]);

}];
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

If you want to make sure you get the date between the span tags, it would be best to be more explicit than either stripping out all the HTML tags and assuming the only thing left is the date, or assuming there is only one span tag in the whole HTML text. It may work for now, but that will likely break in the future if the HTML ever changes.

NSString * timeString = @"<h5 style=\"direction:ltr\"><span data-version-created-date=\"20180326T120530.000+0000\" class=\"releasedDate\">26-Mar-2018 12:05:30</span><span class=\"someOtherClass\">garbageData</span></h5>";
NSRegularExpression *regex = [NSRegularExpression
                              regularExpressionWithPattern:@"<span.*class=\"releasedDate\"[^>]*>(.*)</span.*>"
                              options:NSRegularExpressionCaseInsensitive
                              error:nil];
NSTextCheckingResult *textCheckingResult = [regex firstMatchInString:timeString options:0 range:NSMakeRange(0, timeString.length)];

NSString *releaseDateString = [timeString substringWithRange:[textCheckingResult rangeAtIndex:1]];
if( ! [releaseDateString isEqualToString:@""] )
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MMM-yyyy' 'HH:mm:ss"];
     NSDate *releaseDate = [dateFormatter dateFromString:releaseDateString];

    NSLog( @"%@ - %@", releaseDateString, releaseDate );
}

Note that this works even if there are other spans in the HTML text. It specifically pulls out the one with a class "releasedDate".

wottle
  • 13,095
  • 4
  • 27
  • 68