示例:<img src="图片地址1" /><img src="图片地址2" />
输出:
[图片地址1,图片地址2]
- (NSArray *)filterImageUrlFromHTML:(NSString *)html
{
if (html == nil) {
return nil;
}
NSString *dd = @"<img(.*?)/>|>";
NSMutableArray *resultArray = [NSMutableArray array];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:dd options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *result = [regex matchesInString:html options:NSMatchingReportCompletion range:NSMakeRange(0, html.length)];
for (NSTextCheckingResult *item in result) {
NSString *imgHtml = [html substringWithRange:[item rangeAtIndex:0]];
NSArray *tmpArray = nil;
if ([imgHtml rangeOfString:@"src=\""].location != NSNotFound) {
tmpArray = [imgHtml componentsSeparatedByString:@"src=\""];
} else if ([imgHtml rangeOfString:@"src="].location != NSNotFound) {
tmpArray = [imgHtml componentsSeparatedByString:@"src="];
}
if (tmpArray.count >= 2) {
NSString *src = tmpArray[1];
src = [src stringByReplacingOccurrencesOfString:@"/>" withString:@""];
src = [src stringByReplacingOccurrencesOfString:@">" withString:@""];
[resultArray addObject:src];
}
}
return resultArray;
}