NSString with some html tags, how can I look for the <img> tag and get the contents of the url

I have an NSString with some html tags, how can I search for a tag and get the contents of the url? I'm not sure if I should use Hpple or just a regex expression. In both cases, do I have an example?

+6
source share
1 answer

One way to do this is NSScanner . See this example:

NSString *url = nil; NSString *htmlString = ... NSScanner *theScanner = [NSScanner scannerWithString:htmlString]; // find start of IMG tag [theScanner scanUpToString:@"<img" intoString:nil]; if (![theScanner isAtEnd]) { [theScanner scanUpToString:@"src" intoString:nil]; NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@"\"'"]; [theScanner scanUpToCharactersFromSet:charset intoString:nil]; [theScanner scanCharactersFromSet:charset intoString:nil]; [theScanner scanUpToCharactersFromSet:charset intoString:&url]; // "url" now contains the URL of the img } 
+27
source

Source: https://habr.com/ru/post/890248/


All Articles