简体   繁体   English

如何从Objective-C中的字符串获取url子字符串

[英]How to get a url substring from a string in objective-c

<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? 我得到了类似上面的字符串,我想知道在Objective-C中获取“ http://b.vimeocdn.com/ts/152/946/152946954_200.jpg”子字符串的最佳方法是什么? NSScanner? NSScanner? NSString methods? NSString方法? 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>

Here's another approach: 这是另一种方法:

  • Transform the string into actual HTML. 将字符串转换为实际的HTML。 In other words, conver the &lt; 换句话说,将&lt; and &gt; &gt; stuff into < and > 填充到<>
  • Run it through an NSXMLParser 通过NSXMLParser运行它
  • In the parser delegate , check the attributes dictionary passed into the -parser:didStartElement:namespaceURI:qualifiedName:attributes: method. 解析器委托中 ,检查传递到-parser:didStartElement:namespaceURI:qualifiedName:attributes:方法中的属性字典。
  • 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" . 如果元素名称为@"img" ,则attributes字典应具有一个名为@"src"的键,该键映射到字符串@"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:] 有了字符串后,使用+[NSURL URLWithString:]将其转换为NSURL很简单。

This will work regardless of how the source HTML changes over time. 无论源HTML随时间如何变化,这都将起作用。 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?). 建议的其他方法非常脆弱,因为它们依赖于src所有小写​​字母,并且字符串中的任意位置只有一个src属性(如果有2个,该怎么办?)。 You don't want to parse HTML; 您不想解析HTML; you want to get an attribute out of an XML element. 您想从XML元素中获取属性。 So use the built-in way of doing it! 因此,请使用内置的方法! :) :)

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. strURLImage将是您想要的字符串。

You can use a framework for URL detection and parsing, such as AutoHyperlinks . 您可以使用框架进行URL检测和解析,例如AutoHyperlinks On iOS, you will, of course, have to build it statically or build the source directly into your app. 在iOS上,您当然必须静态地构建它或将源直接构建到您的应用程序中。

Alternatively, for iOS only (currently), use NSDataDetector . 或者,仅对于iOS(当前),使用NSDataDetector Data detectors can find URLs, physical addresses, phone numbers, etc.; 数据检测器可以找到URL,物理地址,电话号码等; you tell it what you'll want from the string, then use the methods of NSRegularExpression to obtain its findings. 您可以从字符串中告诉您所需的内容,然后使用NSRegularExpression的方法获取其发现。

Assuming complete string is called myString, you can do: 假设完整的字符串称为myString,则可以执行以下操作:

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. 我没有测试过,所以我可以用+5代替+6,因此,如果字符串中的字符数少于或等于您想要的字符 ,只需更改最后一行中的数字即可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM