【问题标题】:iOS: Strip <img...> from NSString (a html string)iOS:从 NSString (一个 html 字符串)中剥离 <img...>
【发布时间】:2012-09-11 21:19:54
【问题描述】:

所以我有一个NSString,它基本上是一个带有所有常用html 元素的html 字符串。我想做的具体事情是 从所有 img 标签中删除它。 img 标签可能有也可能没有最大宽度、样式或其他属性,所以我不知道它们的长度。它们总是以/&gt;结尾

我该怎么做?

编辑:基于nicolasthenoz 的回答,我想出了一个需要更少代码的解决方案:

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

【问题讨论】:

    标签: html objective-c ios image


    【解决方案1】:

    您可以在 Swift 4,5 中使用以下函数:

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

    希望对大家有帮助!如果它适合你,请在下面评论。谢谢。

    【讨论】:

      【解决方案2】:

      使用正则表达式,在字符串中找到匹配项并将其删除! 方法如下

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

      【讨论】:

      • 我不太明白为什么在 for 循环中需要所有这些代码。您能解释一下您的解决方案和我的解决方案之间的区别吗? (编辑问题)
      【解决方案3】:

      您可以将NSString 方法stringByReplacingOccurrencesOfStringNSRegularExpressionSearch 选项一起使用:

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

      或者你也可以使用NSRegularExpressionreplaceMatchesInString方法。因此,假设您的 html 位于 NSMutableString *html 中,您可以:

      NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<img[^>]*>"
                                                                             options:NSRegularExpressionCaseInsensitive
                                                                               error:nil];
      
      [regex replaceMatchesInString:html
                            options:0
                              range:NSMakeRange(0, html.length)
                       withTemplate:@""];
      

      我个人倾向于这些选项之一,而不是 RegexKitLitestringByReplacingOccurrencesOfRegex 方法。除非有其他令人信服的问题,否则没有必要为这么简单的事情引入第三方库。

      【讨论】:

        猜你喜欢
        • 2012-09-30
        • 1970-01-01
        • 2011-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-19
        • 2018-04-20
        • 1970-01-01
        相关资源
        最近更新 更多