从字符串中删除HTML标记

时间:2017-01-26 13:25:38

标签: ios swift swift3

从字符串中删除所有HTML标记,例如&nbsp;<p>。我使用下面的代码,但它不起作用。

var content = "<p>&nbsp;&nbsp;test result</p><br/>"; // My String

content.replacingOccurrences(of: "<[^>]+>", with: "", options: String.CompareOptions.regularExpression, range: nil)

但它不会从字符串中删除所有HTML标记。

7 个答案:

答案 0 :(得分:12)

var content = "<p>&nbsp;&nbsp;test result</p><br/>"; // My String

let a = content.replacingOccurrences(of: "<[^>]+>", with: "", options: String.CompareOptions.regularExpression, range: nil)

a将:&nbsp;&nbsp;test result

let b = a.replacingOccurrences(of: "&[^;]+;", with: "", options: String.CompareOptions.regularExpression, range: nil)

b现在为:test result

这也将照顾&lt;等。没有魔力。找出你需要的东西,然后写出适当的RegEx。

答案 1 :(得分:6)

Swift 4测试:删除所有HTML标记并解码实体

提供更稳定的结果

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
    }
}

答案 2 :(得分:4)

在Swift 3.0中的Playground上使用以下扩展程序

extension String {
    var withoutHtmlTags: String {
      return self.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
    }
}

用法

let result = "<strong>HTML</strong> Tags <em>Contain</em> <img /> <a href=\"\">String</a>".withoutHtmlTags

答案 3 :(得分:2)

尝试构建属性字符串:

 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

答案 4 :(得分:2)

为此,我们可以使用

extension String {
    var withoutHtmlTags: String {
    return self.replacingOccurrences(of: "<[^>]+>", with: "", options: 
    .regularExpression, range: nil).replacingOccurrences(of: "&[^;]+;", with: 
    "", options:.regularExpression, range: nil)
    }
}

答案 5 :(得分:1)

我使用了扩展名。扩展的字符串和数据。首先,我将HTML转换为NSAttributedString,然后转换为普通的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 ?? ""
    }
}

示例:

let html = "<div><p>Example</p></div>"
html.htmlToString() //returns example

答案 6 :(得分:0)

添加扩展程序

extension String {

   func removeHTMLTag() -> String {

       return self.replacingOccurrences(of: "<[^>]+>", with: "", options: String.CompareOptions.regularExpression, range: nil)

    }

}

并使用此

let htmlString : String = "<div> <p>I cannot understand </p> </div>"

htmlString.removeHTMLTag() // I cannot understand