Search code examples
iosswiftstringnsattributedstringfoundation

How to remove all tags from html string?


I need to get string without html tags. This is the part of this raw text

.</p>\n\n<p>For a long time, scientists have been opposed to the id

I use

     let htmlData = NSString(string: text).data(using: String.Encoding.unicode.rawValue)
                let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
                        NSAttributedString.DocumentType.html]
                let attributedString = try? NSMutableAttributedString(data: htmlData ?? Data(),
                                                                          options: options,
                                                                          documentAttributes: nil)
   print(attributedString.string)

The issue, that parser delete one \n. I have to get "\n\n For a long ...."

But the result is "\nFor a long time, scientists have been opposed to the idea of describing animals using hu..." This /n is very important.

How to remove all tags from html string ?


Solution

  • extension String{
    var htmlConvertedString : String{
        let string = self.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
        return string
    }}
    

    try this

    and call like this let val = str.htmlConvertedString

    print(val)