Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decode HTML string [duplicate]

Tags:

html

swift

decode

How can I decode my html string from:

<span>Bj&ouml;rn</span>

to

<span>Björn</span>

in Swift 3 ?

like image 524
Kirill Avatar asked Dec 02 '22 12:12

Kirill


1 Answers

Do you really need to preserve the <span> tags, while replacing the &ouml; symbol? One technique, suggested by Leo Dabus in Convert Unicode symbol or its XML/HTML entities into its Unicode number in Swift, converts the symbols includes round-tripping it through an attributed string.

In Swift 4:

extension String {
    /// Converts HTML string to a `NSAttributedString`

    var htmlAttributedString: NSAttributedString? {
        return try? NSAttributedString(data: Data(utf8), options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
    }
}

If you want an attributed string (for example, for use in a UILabel)

let string = "Bj&ouml;rn is <em>great</em> name"
label.attributedText = string.htmlAttributedString

This converts Bj&ouml;rn to Björn and italicizes the <em>...</em> portion, too.

If you just want to convert the HTML symbols and strip out the HTML tags (such as your <span>/</span>), just grab the string:

let string = "Bj&ouml;rn is <em>great</em> name"
if let result = string.htmlAttributedString?.string {
    print(result)   // "Björn is great name"
}

For prior Swift versions, see previous revision of this answer.

like image 63
Rob Avatar answered Dec 04 '22 22:12

Rob