LALIT
LALIT

Reputation: 101

Remove HTML tag from string

Remove all HTML tags like &nbsp;or <p> from string. I used below code but it's not working.

var content = "<p>&nbsp;&nbsp;test result</p><br/>"; // My String

content.replacingOccurrences(of: "<[^>]+>", with: "", options: String.CompareOptions.regularExpression, range: nil)

but it does not remove all HTML tags from string.

Upvotes: 8

Views: 18751

Answers (7)

Daniel Ryan
Daniel Ryan

Reputation: 7100

I used extensions. Extended String and Data. First I convert the HTML to NSAttributedString and then convert to a plain String.

extension String {
    var htmlToAttributedString: NSAttributedString? {
        return Data(utf8).htmlToAttributedString
    }

    var htmlToString: String {
        return htmlToAttributedString?.string ?? ""
    }
}

extension Data {
    var htmlToAttributedString: NSAttributedString? {
        // Converts html to a formatted string.
        do {
            return try NSAttributedString(data: self, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            print("error:", error)
            return nil
        }
    }
    var htmlToString: String {
        return htmlToAttributedString?.string ?? ""
    }
}

Example:

let html = "<div><p>Example</p></div>"
html.htmlToString() //returns example

Upvotes: 2

Pankaj Thakur
Pankaj Thakur

Reputation: 131

For this we can use

extension String {
    var withoutHtmlTags: String {
    return self.replacingOccurrences(of: "<[^>]+>", with: "", options: 
    .regularExpression, range: nil).replacingOccurrences(of: "&[^;]+;", with: 
    "", options:.regularExpression, range: nil)
    }
}

Upvotes: 13

Mahdi Moayeri
Mahdi Moayeri

Reputation: 36

add extension

extension String {

   func removeHTMLTag() -> String {

       return self.replacingOccurrences(of: "<[^>]+>", with: "", options: String.CompareOptions.regularExpression, range: nil)

    }

}

and use this

let htmlString : String = "<div> <p>I cannot understand </p> </div>"

htmlString.removeHTMLTag() // I cannot understand 

Upvotes: 1

192kb
192kb

Reputation: 685

Swift 4 tested: Removes all HTML tags and decodes entities

Provides more stable result

extension String {
    public var withoutHtml: String {
        guard let data = self.data(using: .utf8) else {
            return self
        }

        let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
            .documentType: NSAttributedString.DocumentType.html,
            .characterEncoding: String.Encoding.utf8.rawValue
        ]

        guard let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) else {
            return self
        }

        return attributedString.string
    }
}

Upvotes: 15

Pravin Kamble
Pravin Kamble

Reputation: 853

Use Following Extension tested on Playground in Swift 3.0

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

Usage

let result = "<strong>HTML</strong> Tags <em>Contain</em> <img /> <a href=\"\">String</a>".withoutHtmlTags

Upvotes: 8

Gabriel
Gabriel

Reputation: 3359

Try to build an attributed string:

 let data = content.data(using: .utf8)
 let options = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType] as [String : Any]
 let attrStr = try NSAttributedString(data:data!, options:options ,documentAttributes:nil)
 content = attrStr.string

Upvotes: 4

Idan
Idan

Reputation: 5450

var content = "<p>&nbsp;&nbsp;test result</p><br/>"; // My String

let a = content.replacingOccurrences(of: "<[^>]+>", with: "", options: String.CompareOptions.regularExpression, range: nil)

a will be: &nbsp;&nbsp;test result

let b = a.replacingOccurrences(of: "&[^;]+;", with: "", options: String.CompareOptions.regularExpression, range: nil)

b will now be: test result

This will also take care of &lt; and so on. There is no magic. Find out what you need and then write the proper RegEx.

Upvotes: 21

Related Questions