2017-03-07 12 views
1

にプレーン読める文字列に変換まあ、私はキーと値のペアを持つJSONレスポンスを持っていますが、キーの値のいずれかで、私はこの文字列を取得しています: -削除HTMLのキーワードやタグとスウィフト3

%3chtml%3e%3cbodyスタイル%3d%27背景色%3a%27%3e%3cPスタイル%3d%22MARGIN%3a 0in%0pt%22%3e%3cSPANスタイル%3d%22FONT-FAMILY%3a%27Trebuchet MS %27%2c%27sans-serif%27%3b%FONT-SIZE%3a 10%%22%3e貴重なクライアント%2c%3c%3fxml%3前置詞の接頭辞%3d%22%%3aschemas-microsoft-com%3aoffice% 3オフィシャル%22%2%3%3%3%3%3%3%3%3%3%3%2%3%3%3%2%3%3%3%3%22%3% 22%3e%3co%3ap%3e%26nbsp%3b%3c%2fo%3ap%3e%3c%2fP%3e%0d%0a%3cPスタイル%3d%22MARGIN%3a 0in%0pt%22%3e%3cSPANスタイル%3d%22FONT-FAMILY%3a%27トリブ他のMS%27%2C%の27sans-セリフ%27%3bのFONT-SIZEの%3aは10ptの%22%3eWeは現在、当社の顧客満足度調査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 ?? "" 
    } 
} 
+0

属性付き文字列はDear値のクライアントに記録されていますか?現在、顧客満足度調査は2014年ですか? –

+0

いいえ、同じ文字列をもう一度記録しています。変更はありません。すべてのタグとキーワードはそのままです.. – Vizllx

答えて

2

HTML文字列は以下のコードを使用し得るために: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)") 

をこのコードを試してみてください以下のコード

let rawstr = htmlstring.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil) 
    print("rawstr \(rawstr)") 
+0

優れた仕事! @Vizllx。 – Vizllx

+0

。あなたは大歓迎です。文字列に%記号がある場合は、%エンコードの付加情報。 – Vinodh

0

を使用する - :そのようなものの一つがこれですstringextensionを使用する代わりに、私は現在、私のプロジェクトでこのコードを使用しています。

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) 
} 
関連する問題