I'm looking for ways to locate a range of html image tag and change it. Didn't find any good solutions on the net, so hoping you guys can help me.

Example string:

NSString *htmlString = @"<p><a href="/url/10/512.png"><img src="/url/10/512.png" alt=" " border="0" /></a></p><div style="clear:both;"></div>";

How to find and change img src="/url/10/512.png".

img source is dynamic and always different, so I'm looking for an optimal solution.

The best way I can come up with is to find img src=" with rangeOfString, and then loop through string from that range and find a qoute mark " then I would know the full range, and could change it.

But this seems nasty, and I think better solutions are around. Maybe someone knows how or knows some links where I could find something useful?

EDIT: sorry I didn't mentioned, my string can be longer and consist more than one img tag e.g

NSString *htmlString = @"<p><a href="/url/10/512.png"><img src="/url/10/512.png" alt=" " border="0" /></a><a href="/url/10/513.png"><img src="/url/10/513.png" alt=" " border="0" /></a></p><div style="clear:both;"></div>";

Thanks in advance!

有帮助吗?

解决方案

If you replace the part where you loop for the closing double-quote with a call to rangeOfString:options:range:, passing the range from the end of img src=" substring plus one to the end of the string, your solution would no longer "seem nasty". Of course you could always use regex to look for img src=\"([^\"]*)\", but that may need a few extra lines of code:

NSString *str = @"<p><a href=\"/url/10/512.png\"><img src=\"/url/10/512.png\" alt=\" \" border=\"0\" /></a></p><div style=\"clear:both;\"></div>";
NSError *err;
NSRegularExpression *regex = [NSRegularExpression
    regularExpressionWithPattern:@"img src=\"([^\"]*)\""
    options:NSRegularExpressionCaseInsensitive
    error:&err];
NSTextCheckingResult *m = [regex firstMatchInString:str options:0 range:NSMakeRange(0, str.length)];
if (!NSEqualRanges(m.range, NSMakeRange(NSNotFound, 0))) {
    NSLog(@"%@", [str substringWithRange:[m rangeAtIndex:1]]);
}

This produces the output of

/url/10/512.png

my string can be longer and consist more than one img tag

With the regex above, you can get them all by calling matchesInString:, and looping over the results.

其他提示

You can create a category for NSString. Especially, if this action is going to be used in different classes.

@interface NSString (Utils)
- (NSString *)replaceStringPattern:(NSString *)searchPattern withString:(NSString *)replacementString;
@end

@implementation NSString (Utils)

- (NSString *)replaceStringPattern:(NSString *)searchPattern withString:(NSString *)replacementString
{
    NSRange range = NSMakeRange(0, self.length);

    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:searchPattern options:NSRegularExpressionCaseInsensitive error:&error];
    if (error)
    {
        NSLog(@"ERROR: Regular expression cannot be created for pattern %@", searchPattern);
    }
    return [regex stringByReplacingMatchesInString:self options:0 range:range withTemplate:replacementString];
}

@end

Both rangeOfString: method and NSRegularExpression class can be used (I prefer the last one). Then, you can invoke it from anywhere in your project as following:

    NSString *htmlString = @"<p><a href=\"/url/10/512.png\"><img src=\"/url/10/512.png\" alt=\" \" border=\"0\" /></a></p><div style=\"clear:both;\"></div>";
    NSString *newImgSrc = @"/url/10/512_new.png";
    NSString *newHtmlString = [htmlString replaceStringPattern:@"img src=\"([^\"]*)\"" 
            withString:[NSString stringWithFormat:@"img src=\"%@\"", newImgSrc]];

Try This...

htmlString = [htmlString substringFromIndex:[htmlString rangeOfString:@"img src="].location];
NSRange range = [htmlString rangeOfString:@".png\""];
htmlString = [htmlString substringToIndex:(range.location + range.length)];
NSLog(@"%@",htmlString);

You can use this NSString method

- (NSRange)rangeOfString:(NSString *)aString

You can find NSRange of String, that you want and get data from this range.

HPH.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top