Zoltán Matók
Zoltán Matók

Reputation: 4045

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:@""]; 

Upvotes: 8

Views: 6633

Answers (3)

Sour LeangChhean
Sour LeangChhean

Reputation: 7409

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.

Upvotes: 1

nicolasthenoz
nicolasthenoz

Reputation: 1852

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);
}

Upvotes: 4

Rob
Rob

Reputation: 437592

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.

Upvotes: 14

Related Questions