Sheehan Alam Sheehan Alam - 8 months ago 113
Objective-C Question

How can I parse the src property of an iframe tag in Objective-C?

I have the following string:

<iframe width="1280" height="720" src="http://www.youtube.com/embed/Pb55ep-DrSo?wmode=opaque&autoplay=1&fs=1&feature=oembed&showinfo=0&autohide=1&controls=0" frameborder="0" allowfullscreen></iframe>


I'd like to pull out the src property, but not exactly sure how I would parse that in Objective-C?

Answer Source

This is ugly, but it works:

NSString* str = @"<iframe width=\"1280\" height=\"720\" src=\"http://www.youtube.com/embed/Pb55ep-DrSo?wmode=opaque&autoplay=1&fs=1&feature=oembed&showinfo=0&autohide=1&controls=0\" frameborder=\"0\" allowfullscreen></iframe>";
str = [str substringFromIndex:[str rangeOfString:@"src=\""].location+[str rangeOfString:@"src=\""].length];
str = [str substringToIndex:[str rangeOfString:@"\""].location ];
NSLog(@"Str %@",str);

I tested it, and it outputs:

2012-08-17 09:16:55.285 TEST[24413:c07] Str http://www.youtube.com/embed/Pb55ep-DrSo?wmode=opaque&autoplay=1&fs=1&feature=oembed&showinfo=0&autohide=1&controls=0
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download