Reputation: 101
Remove all HTML tags like
or <p>
from string. I used below code but it's not working.
var content = "<p> 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
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
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
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
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
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
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
Reputation: 5450
var content = "<p> test result</p><br/>"; // My String
let a = content.replacingOccurrences(of: "<[^>]+>", with: "", options: String.CompareOptions.regularExpression, range: nil)
a will be: 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 <
and so on. There is no magic. Find out what you need and then write the proper RegEx.
Upvotes: 21