[英]remove HTML keywords or tags and convert to plain readable string in Swift 3
Well, I have a json response with a key-value pair but in one of the key's value, I am getting this string:- 好吧,我有一个带有键值对的json响应,但在其中一个键的值中,我得到这个字符串: -
%3chtml%3e%3cbody style%3d%27background-color%3a%27%3e%3cP style%3d%22MARGIN%3a 0in 0in 0pt%22%3e%3cSPAN style%3d%22FONT-FAMILY%3a %27Trebuchet MS%27%2c%27sans-serif%27%3b FONT-SIZE%3a 10pt%22%3eDear valued clients%2c %3c%3fxml%3anamespace prefix %3d o ns %3d %22urn%3aschemas-microsoft-com%3aoffice%3aoffice%22 %2f%3e%3co%3ap%3e%3c%2fo%3ap%3e%3c%2fSPAN%3e%3c%2fP%3e%0d%0a%3cP style%3d%22MARGIN%3a 0in 0in 0pt%22%3e%3co%3ap%3e%26nbsp%3b%3c%2fo%3ap%3e%3c%2fP%3e%0d%0a%3cP style%3d%22MARGIN%3a 0in 0in 0pt%22%3e%3cSPAN style%3d%22FONT-FAMILY%3a %27Trebuchet MS%27%2c%27sans-serif%27%3b FONT-SIZE%3a 10pt%22%3eWe are currently conducting our customer satisfaction survey 2014
%3chtml%3e%3cbody style%3d%27background-color%3a%27%3e%3cP style%3d%22MARGIN%3a 0in 0in 0pt%22%3e%3cSPAN style%3d%22FONT-FAMILY%3a%27Trebuchet MS% 27%2c%27sans-serif%27%3b FONT-SIZE%3a 10pt%22%3e尊重客户%2c%3c%3fxml%3anamespace前缀%3d o ns%3d%22urn%3aschemas-microsoft-com%3aoffice%3aoffice %22%2f%3e%3co%3ap%3e%3c%2fo%3ap%3e%3c%2fSPAN%3e%3c%2fP%3e%0d%0a%3cP样式%3d%22MARGIN%3a 0in 0in 0pt%22 %3e%3co%3ap%3e%26nbsp%3b%3c%2fo%3ap%3e%3c%2fP%3e%0d%0a%3cP样式%3d%22MARGIN%3a 0in 0in 0pt%22%3e%3cSPAN样式% 3d%22FONT-FAMILY%3a%27Trebuchet MS%27%2c%27sans-serif%27%3b FONT-SIZE%3a 10pt%22%3e我们目前正在进行2014年客户满意度调查
I need only the string here not any tags or keywords means I need only the relevant string, like this:- 我只需要字符串这里没有任何标签或关键字意味着我只需要相关的字符串,如下所示: -
Dear valued clients We are currently conducting our customer satisfaction survey 2014
尊敬的客户我们目前正在进行2014年的客户满意度调查
Well I have tried using many encoding as mentioned in other answers available in stackoverflow but I failed in all. 好吧,我已尝试使用stackoverflow中提供的其他答案中提到的许多编码,但我都失败了。 One of such is this :-
其中之一是: -
extension String {
var html2AttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return nil }
do {
return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}
To get HTML string use the below code: 要获取HTML字符串,请使用以下代码:
var htmlstring = "%3chtml%3e%3cbody style%3d%27background-color%3a%27%3e%3cP style%3d%22MARGIN%3a 0in 0in 0pt%22%3e%3cSPAN style%3d%22FONT-FAMILY%3a %27Trebuchet MS%27%2c%27sans-serif%27%3b FONT-SIZE%3a 10pt%22%3eDear valued clients%2c %3c%3fxml%3anamespace prefix %3d o ns %3d %22urn%3aschemas-microsoft-com%3aoffice%3aoffice%22 %2f%3e%3co%3ap%3e%3c%2fo%3ap%3e%3c%2fSPAN%3e%3c%2fP%3e%0d%0a%3cP style%3d%22MARGIN%3a 0in 0in 0pt%22%3e%3co%3ap%3e%26nbsp%3b%3c%2fo%3ap%3e%3c%2fP%3e%0d%0a%3cP style%3d%22MARGIN%3a 0in 0in 0pt%22%3e%3cSPAN style%3d%22FONT-FAMILY%3a %27Trebuchet MS%27%2c%27sans-serif%27%3b FONT-SIZE%3a 10pt%22%3eWe are currently conducting our customer satisfaction survey 2014"
htmlstring = htmlstring.removingPercentEncoding!
print("html string \(htmlstring)")
To get only string inside HTML use the below code 要仅在HTML中获取字符串,请使用以下代码
let rawstr = htmlstring.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
print("rawstr \(rawstr)")
Try this code out instead of using the string
extension
. 尝试使用此代码而不是使用
string
extension
。 I am currently using this code in my project. 我目前在我的项目中使用此代码。
do {
let attributedString = try NSAttributedString(data: self.alarmTitle[indexPath.row].data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil)
var urlString = attributedString.string
}
catch
{
print(error)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.