Skip to content

Instantly share code, notes, and snippets.

@elmodos
Created July 4, 2019 10:40
Show Gist options
  • Save elmodos/759563a5eec77e66aa7b6bf799edceac to your computer and use it in GitHub Desktop.
Save elmodos/759563a5eec77e66aa7b6bf799edceac to your computer and use it in GitHub Desktop.
Swift: strip String from HTML tags
public extension String {
func htmlTagsStripped() -> String? {
let stripped = self
.flattenHtml()
.trimmingCharacters(in: .whitespacesAndNewlines)
.removingLinebreaks()
.removingMultipleSpaces()
.replacingHtmlCharEntities()
return stripped
}
}
public extension String {
func flattenHtml() -> String {
var html = self
var text: NSString? = nil
let scanner = Scanner(string: html)
while !scanner.isAtEnd {
scanner.scanUpTo("<", into: nil)
scanner.scanUpTo(">", into: &text)
if let text = text {
html = html.replacingOccurrences(of: "\(text)>", with: " ")
}
}
return html
}
func removingLinebreaks() -> String {
return self.replacingOccurrences(of: "\n", with: " ")
}
func removingMultipleSpaces() -> String {
return self
.components(separatedBy: " ")
.filter { $0.count > 0 }
.map { String($0) }
.joined(separator: " ")
}
func replacingHtmlCharEntities() -> String {
let map = [
"&nbsp;" : " ",
"&lt;" : "<",
"&gt;" : ">",
"&amp;" : "&",
"&quot;" : "\"",
"&apos;" : "'",
"&cent;" : "¢",
"&pound;" : "£",
"&yen;" : "¥",
"&euro;" : "€",
"&copy;" : "©",
"&reg;" : "®",
"&rsquo;" : "`"
]
var result = self
map.enumerated().forEach { (_, element) in
result = result.replacingOccurrences(of: element.key, with: element.value)
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment