Skip to content

Instantly share code, notes, and snippets.

@Mahabali
Created August 26, 2017 09:40
Show Gist options
  • Save Mahabali/aaa639594ab25a797341694e064ec747 to your computer and use it in GitHub Desktop.
Save Mahabali/aaa639594ab25a797341694e064ec747 to your computer and use it in GitHub Desktop.
iOS HTML characters decoding
+ (NSString*)decodeHTMLCharacters:(NSString*)sampleUnFormatterString{
if (sampleUnFormatterString == nil || sampleUnFormatterString.length == 0) {
return sampleUnFormatterString;
}
NSString *sampleUnFormatterStringCopy =sampleUnFormatterString;
NSString *regString = [[NSString alloc]initWithFormat:@"&#[0123456789]{2,4};"];
NSError *error=nil;
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:regString
options:0
error:&error];
NSArray* matches = [regex matchesInString:sampleUnFormatterString options:0 range: NSMakeRange(0, [sampleUnFormatterString length])];
for (NSTextCheckingResult* match in matches) {
NSString* matchText = [sampleUnFormatterString substringWithRange:[match range]];
NSString *strippedText = [matchText stringByReplacingOccurrencesOfString:@"&#" withString:@""];
strippedText = [strippedText stringByReplacingOccurrencesOfString:@";" withString:@""];
strippedText = [NSString stringWithFormat:@"%C",(unichar)strippedText.integerValue];
sampleUnFormatterStringCopy= [sampleUnFormatterStringCopy stringByReplacingOccurrencesOfString:matchText withString:strippedText];
}
NSLog(@"Formatted String %@",sampleUnFormatterStringCopy);
return sampleUnFormatterStringCopy;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment