gpt4 book ai didi

iphone - 从字符串中检索图像 url

转载 作者:可可西里 更新时间:2023-11-01 05:10:21 25 4
gpt4 key购买 nike

我正在解析一个 xml 文件,我可以 NSLog 解析,但我的问题是我需要从这个“字符串”中获取图像 url:

<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>

对不起,纯代码:)

我用来提取 url 的是这段代码,但它不起作用:

   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

关于iphone - 从字符串中检索图像 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9278195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com