Skip to content Skip to sidebar Skip to footer

Ios: Strip 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 t

Solution 1:

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.

Solution 2:

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

Solution 3:

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.

Post a Comment for "Ios: Strip From Nsstring (a Html String)"