NSString带有一些html标记,如何搜索< img>标记并获取url的内容 [英] NSString with some html tags, how can I search for <img> tag and get the content of url

查看:84
本文介绍了NSString带有一些html标记,如何搜索< img>标记并获取url的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些html标记的NSString,如何搜索标记并获取url的内容?我不确定是否必须使用Hpple或简单的Regex表达式.在这两种情况下,我都可以举一些例子吗?

I have a NSString with some html tags, how can I search for tag and get the content of url? I'm not sure if I must use Hpple or a simple Regex expression. In both cases can I have some example?

推荐答案

一种方法是使用

One way to do this is using NSScanner. See this example:

NSString *url = nil;
NSString *htmlString = ...
NSScanner *theScanner = [NSScanner scannerWithString:htmlString];
// find start of IMG tag
[theScanner scanUpToString:@"<img" intoString:nil];
if (![theScanner isAtEnd]) {
    [theScanner scanUpToString:@"src" intoString:nil];
    NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@"\"'"];
    [theScanner scanUpToCharactersFromSet:charset intoString:nil];
    [theScanner scanCharactersFromSet:charset intoString:nil];
    [theScanner scanUpToCharactersFromSet:charset intoString:&url];
    // "url" now contains the URL of the img
}

这篇关于NSString带有一些html标记,如何搜索&lt; img&gt;标记并获取url的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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