1
<p><a href="http://vimeo.com/23486376" title="Rebecca Black's Friday on Rock Band"><img src="http://b.vimeocdn.com/ts/152/946/152946954_200.jpg" alt="Rebecca Black's Friday on Rock Band" /></a></p><p></p><p>Cast: <a href="http://vimeo.com/thenerdery" style="color: #2786c2; text-decoration: none;">The Nerdery</a></p>

I got a string like above, I am wondering what's the best way in objective-c to get the "http://b.vimeocdn.com/ts/152/946/152946954_200.jpg" substring? NSScanner? NSString methods? Thanks!

update: the string is actually:

<p><a href="http://vimeo.com/23333305" title="Ad League Bowling Championship"><img src="http://b.vimeocdn.com/ts/151/787/151787049_200.jpg" alt="Ad League Bowling Championship" /></a></p><p></p><p>Cast: <a href="http://vimeo.com/thenerdery" style="color: #2786c2; text-decoration: none;">The Nerdery</a></p>

4 Answers 4

5

Here's another approach:

  • Transform the string into actual HTML. In other words, conver the &lt; and &gt; stuff into < and >
  • Run it through an NSXMLParser
  • In the parser delegate, check the attributes dictionary passed into the -parser:didStartElement:namespaceURI:qualifiedName:attributes: method.
  • If the element name is @"img", then the attributes dictionary should have a key called @"src" that maps to the string @"http://b.vimeocdn.com/ts/152/946/152946954_200.jpg".
  • Once you have the string, it's trivial to transform it into an NSURL using +[NSURL URLWithString:]

This will work regardless of how the source HTML changes over time. The other approaches suggested are extremely fragile, because they rely on things like src being all lowercase and there only being a single src attribute anywhere in the string (what if you have 2?). You don't want to parse HTML; you want to get an attribute out of an XML element. So use the built-in way of doing it! :)

Sign up to request clarification or add additional context in comments.

1 Comment

Looks like that's THE way to do it. But I used my quick and dirty way in the end though. Thanks for the great answer.
2
NSString *strComplete = @"&lt;p>&lt;a href="http://vimeo.com/23486376" title="Rebecca Black's Friday on Rock Band">&lt;img src="http://b.vimeocdn.com/ts/152/946/152946954_200.jpg" alt="Rebecca Black's Friday on Rock Band" />&lt;/a>&lt;/p>&lt;p>&lt;/p>&lt;p>Cast: &lt;a href="http://vimeo.com/thenerdery" style="color: #2786c2; text-decoration: none;">The Nerdery&lt;/a>&lt;/p>";   
NSArray *arrComplete = [strComplete componentSeparatedBy:@"src="]; 
NSString *strSecond = [arrComplete objectAtIndex:1];  
NSArray *arrSecond = [strSecond componentSeparatedBy:@" alt"];  
NSString *strURLImage = [arrSecond objectAtIndex:0];  

strURLImage will be your desired string.

1 Comment

this makes more sense to me and might be more efficient than my solution. Thanks!
1

You can use a framework for URL detection and parsing, such as AutoHyperlinks. On iOS, you will, of course, have to build it statically or build the source directly into your app.

Alternatively, for iOS only (currently), use NSDataDetector. Data detectors can find URLs, physical addresses, phone numbers, etc.; you tell it what you'll want from the string, then use the methods of NSRegularExpression to obtain its findings.

Comments

0

Assuming complete string is called myString, you can do:

NSRange srcRange = [myString rangeOfString:@"src="];
NSRange endrange = [myString rangeOfString:@"\"" options:nil range:
   NSMakeRange(srcRange.location + 5, [mystring count] - srcRange.location - 6)];
NSString *url = [myString subStringWithRange:
   NSMakeRange(srcRange.location + 5, srcRange.location + 5 + endRange.lenght)];

I haven't tested that so I could have put a +5 instead of a +6, so if the string has got some chars less or more than you want, just change the numbers in the last line.

2 Comments

I figured I would use this instead in the end: ` NSString *imgString = [[NSString alloc] init]; NSScanner *scanner = [NSScanner scannerWithString:descriptionString]; [scanner scanUpToString:@"img src=&quot;" intoString:nil]; [scanner scanUpToString:@"http" intoString:nil]; [scanner scanUpToString:@"&quot; alt" intoString:&imgString];` Thanks anyway.
Very happy if you found a solution yourself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.