Skip to content

Instantly share code, notes, and snippets.

@eonist
Forked from ollieatkinson/String+HTML.swift
Created March 2, 2017 22:10
Show Gist options
  • Save eonist/50ff67be7558f8a093f225c2fe84678f to your computer and use it in GitHub Desktop.
Save eonist/50ff67be7558f8a093f225c2fe84678f to your computer and use it in GitHub Desktop.
Unescape HTML entities in Swift 3 == £ -> £
extension String {
func unescapeHTMLEntities() throws -> String {
guard contains("&#") else {
return self
}
guard let data = data(using: .utf8) else {
return self
}
let attributes: [String: Any] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8
]
let attributedString = try NSAttributedString(
data: data,
options: attributes,
documentAttributes: nil
)
return attributedString.string
}
}
"£100".unescapeHTMLEntities() // £100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment