从字符串中检索图片网址 [英] Retrieve image url from string

查看:53
本文介绍了从字符串中检索图片网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析一个xml文件,我可以使用NSLog进行解析,但是我的问题是我需要从此字符串"中获取图片网址:

I'm parsing an xml file and I can NSLog the parsing, but my problem is that I need to get the image url`s from this "string":

<p>
 <a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43.jpg"><img class="alignnone size-thumbnail wp-image-81" title="ex4" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43-150x150.jpg" alt="" width="150" height="150" /></a>
 <a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32.jpg"><img class="alignnone size-thumbnail wp-image-80" title="ex3" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32-150x150.jpg" alt="" width="150" height="150" /></a>
 <a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23.jpg"><img class="alignnone size-thumbnail wp-image-79" title="ex2" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23-150x150.jpg" alt="" width="150" height="150" /></a>
 <a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12.jpg"><img class="alignnone size-thumbnail wp-image-71" title="ex1" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12-150x150.jpg" alt="" width="150" height="150" /></a>
 </p>

对不起,您的明码:)

此代码用来提取网址的即时消息是什么,但它不起作用:

what im using to extract the url´s is this code but its not working:

   NSRange start = [item.imageGallery       rangeOfString:@"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/"];
    NSRange end = [item.imageGallery rangeOfString:@"\" "];

    int rangeLength = (int)(end.location - start.location);

    NSString *hrefString = [[NSString alloc] initWithString:[item.imageGallery substringWithRange:NSMakeRange(start.location, rangeLength)]];
    NSLog(@"image url = %@",hrefString);

推荐答案

使用正则表达式:"src = \"([[^ \] +)\""

这是一些示例代码:

NSString *searchedString = @""  
    @"<p>"
    @"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43.jpg\"><img class=\"alignnone size-thumbnail wp-image-81\" title=\"ex4\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
    @"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32.jpg\"><img class=\"alignnone size-thumbnail wp-image-80\" title=\"ex3\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
    @"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23.jpg\"><img class=\"alignnone size-thumbnail wp-image-79\" title=\"ex2\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
    @"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12.jpg\"><img class=\"alignnone size-thumbnail wp-image-71\" title=\"ex1\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
    @"</p>";
NSRange rangeOfString = NSMakeRange(0, [searchedString length]);
//NSLog(@"searchedString: %@", searchedString);

NSString *pattern = @"src=\"([^\"]+)\"";
NSError* error = nil;

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSArray *matchs = [regex matchesInString:searchedString options:0 range:rangeOfString];
    for (NSTextCheckingResult* match in matchs) {
        NSLog(@"url: %@", [searchedString substringWithRange:[match rangeAtIndex:1]]);
    }

NSLog输出:

url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43-150x150.jpg
url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32-150x150.jpg
url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23-150x150.jpg
url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12-150x150.jpg

这篇关于从字符串中检索图片网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆