Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 删除HTML关键字或标记,并在Swift 3中转换为易读字符串_Ios_Swift - Fatal编程技术网

Ios 删除HTML关键字或标记,并在Swift 3中转换为易读字符串

Ios 删除HTML关键字或标记,并在Swift 3中转换为易读字符串,ios,swift,Ios,Swift,我有一个带有键值对的json响应,但在其中一个键值中,我得到以下字符串:- %3chtml%3e%3cbody style%3d%27背景色%3a%27%3e%3cP style%3d%22MARGIN%3a 0in 0in 0pt%22%3e%3cSPAN style%3d%22FONT-FAMILY%3a%27Trebuchet MS%27%2c%27sans衬线%27%3b字体大小%3a 10pt%22%3eDAR值客户端%2c%3c%3fxml%3anamespace前缀%3d对象%3d

我有一个带有键值对的json响应,但在其中一个键值中,我得到以下字符串:-

%3chtml%3e%3cbody style%3d%27背景色%3a%27%3e%3cP style%3d%22MARGIN%3a 0in 0in 0pt%22%3e%3cSPAN style%3d%22FONT-FAMILY%3a%27Trebuchet MS%27%2c%27sans衬线%27%3b字体大小%3a 10pt%22%3eDAR值客户端%2c%3c%3fxml%3anamespace前缀%3d对象%3d%22urn%3aoffice%22%2f%3e%3co%3ap%3e%3c%2fo%3ap%3e%3c%2fP%3e%0d%0a%3cP样式%3d%22MARGIN%3a 0in 0pt%22%3e%3co%3ap%3e%26nbsp%3b%3c%2fo%3ap%3e%3e%0d%0a%3cP样式%3d%22MARGIN%3a 0in 0in 0pt%22%3e%3c样式%3d%22MARGIN%3c%3c%3d%3c%3c%3b当前,我们的客户满意度为273b%3b%2014年调查

这里我只需要字符串,不需要任何标记或关键字意味着我只需要相关字符串,如下所示:-

亲爱的尊贵客户,我们目前正在进行2014年客户满意度调查

我试过使用stackoverflow中其他答案中提到的许多编码,但都失败了。其中之一是:-

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

要获取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)")
要仅获取HTML中的字符串,请使用以下代码

    let rawstr = htmlstring.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
    print("rawstr \(rawstr)")
let rawstr=htmlstring.replacingOccurrences(of:“]+>”,with:,选项:。正则表达式,范围:nil)
打印(“rawstr\(rawstr)”)

尝试此代码,而不是使用
字符串
扩展名
。我目前正在我的项目中使用此代码

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

我们目前正在进行2014年客户满意度调查??不,它只是再次记录相同的字符串。。没有变化。。所有的标签和关键字都完好无损。干得好@维兹利克斯。不客气。附加信息如果字符串有%symbol,则为百分比编码。