Summary of methods of removing HTML tags on IOS

Time:2021-4-19

preface

In some development, it is necessary to filter out the HTML tags in the text input by users to prevent XSS attacks. This article will introduce in detail the relevant content of removing HTML tags from IOS, and share it for your reference. Let’s take a look at the detailed introduction.

The data returned by the request interface contains HTML tags. The method removed from OC has been done before. The code is as follows


-(NSString *)filterHTML:(NSString *)html{
  NSScanner * scanner = [NSScanner scannerWithString:html];
  NSString * text = nil;
  while([scanner isAtEnd]==NO)
  {
    [scanner scanUpToString:@"<" intoString:nil];
    [scanner scanUpToString:@">" intoString:&text];
    html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
  }
  return html;
}

You can also use regular filters


-(NSString *)getZZwithString:(NSString *)string{
 NSRegularExpression *regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|\n"
                                         options:0
                                          error:nil];
 string=[regularExpretion stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];
 return string;
}

It can also be converted to rich text


+ (NSMutableAttributedString *)praseHtmlStr:(NSString *)htmlStr {
  NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
  [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, attributedString.length)];
  [attributedString addAttribute:NSForegroundColorAttributeName value:CommonColor(Color333333) range:NSMakeRange(0, attributedString.length)];
 
  return attributedString;
}

But this time I used swift. If you look at the methods I collected, they are almost the same

func removeHTML(htmlString : String)->String{
    return htmlString.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
}
 
extension String {
  func deleteHTMLTag(tag:String) -> String {
    return self.replacingOccurrences(of: "(?i)</?\(tag)\b[^<]*>", with: "", options: .regularExpression, range: nil)
  }
  
  func deleteHTMLTags(tags:[String]) -> String {
    var mutableString = self
    for tag in tags {
      mutableString = mutableString.deleteHTMLTag(tag: tag)
    }
    return mutableString
  }
  
  ///Remove string labels
  mutating func filterHTML() -> String?{
    let scanner = Scanner(string: self)
    var text: NSString?
    while !scanner.isAtEnd {
      scanner.scanUpTo("<", into: nil)
      scanner.scanUpTo(">", into: &text)
      self = self.replacingOccurrences(of: "\(text == nil ? "" : text!)>", with: "")
    }
    return self
  }
}

summary

The above is the whole content of this article, there are many deficiencies in this article, I hope the content of this article has a certain reference learning value for your study or work, if you have any questions, you can leave a message to exchange, thank you for your support for developer.