Decode HTML string [duplicate]

天大地大妈咪最大 提交于 2019-11-28 08:57:00

问题


This question already has an answer here:

  • Convert Unicode symbol or its XML/HTML entities into its Unicode number in Swift 2 answers

How can I decode my html string from:

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

to

<span>Björn</span>

in Swift 3 ?


回答1:


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.



来源:https://stackoverflow.com/questions/40113805/decode-html-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!