简体   繁体   中英

iOS: Strip <img…> from NSString (a html string)

So I have an NSString which is basically an html string with all the usual html elements. The specific thing I would like to do is to just strip it from all the img tags. The img tags may or may not have max-width, style or other attributes so I do not know their length up front. They always end with />

How could I do this?

EDIT: Based on nicolasthenoz 's answer, I came up with a solution that requires less code:

NSString *HTMLTagss = @"<img[^>]*>"; //regex to remove img tag
NSString *stringWithoutImage = [htmlString stringByReplacingOccurrencesOfRegex:HTMLTagss withString:@""]; 

You can use the NSString method stringByReplacingOccurrencesOfString with the NSRegularExpressionSearch option:

NSString *result = [html stringByReplacingOccurrencesOfString:@"<img[^>]*>" withString:@"" options:NSCaseInsensitiveSearch | NSRegularExpressionSearch range:NSMakeRange(0, [html length])];

Or you can also use the replaceMatchesInString method of NSRegularExpression . Thus, assuming you have your html in a NSMutableString *html , you can:

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<img[^>]*>"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:nil];

[regex replaceMatchesInString:html
                      options:0
                        range:NSMakeRange(0, html.length)
                 withTemplate:@""];

I'd personally lean towards one of these options over the stringByReplacingOccurrencesOfRegex method of RegexKitLite . There's no need to introduce a third-party library for something as simple as this unless there was some other compelling issue.

Use a regular expression, find the matchs in your string and remove them ! Here is how

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<img[^>]*>"
                                                                       options:NSRegularExpressionCaseInsensitive 
                                                                         error:nil];

NSMutableString* mutableString = [yourStringToStripFrom mutableCopy];
NSInteger offset = 0; // keeps track of range changes in the string due to replacements.
for (NSTextCheckingResult* result in [regex matchesInString:yourStringToStripFrom 
                                                    options:0 
                                                      range:NSMakeRange(0, [yourStringToStripFrom length])]) {

    NSRange resultRange = [result range];   
    resultRange.location += offset; 

    NSString* match = [regex replacementStringForResult:result 
                                               inString:mutableString 
                                                 offset:offset 
                                               template:@"$0"];

    // make the replacement
    [mutableString replaceCharactersInRange:resultRange withString:@""];

    // update the offset based on the replacement
    offset += ([match length] - resultRange.length);
}

You can use below function in Swift 4,5:

func filterImgTag(text: String) -> String{
    return text.replacingOccurrences(of: "<img[^>]*>", with: "", options: String.CompareOptions.regularExpression)
}

Hope it can help you all! comment below if it work for you. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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