Ios extracts the img address (image address) from the html string ),

Source: Internet
Author: User

Ios extracts the img address (image address) from the html string ),

 

 

Original article address http://www.cnblogs.com/qianLL/p/6082287.html

Sometimes the background returns an html string. We need to extract the image address from it. This is the correct regular expression.

That is

<(Img | IMG )(.*?) (/> |> </Img> |>)

 

The code below returns all the image addresses in the string, all of which are a set.

+ (NSArray *)filterImage:(NSString *)html{    NSMutableArray *resultArray = [NSMutableArray array];        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<(img|IMG)(.*?)(/>|></img>|>)" options:NSRegularExpressionAllowCommentsAndWhitespace 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];                                NSUInteger loc = [src rangeOfString:@"\""].location;                if (loc != NSNotFound) {                    src = [src substringToIndex:loc];                    [resultArray addObject:src];                }            }        }    return resultArray;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.