2017-06-26 90 views
-3

我有一個編碼字符串,我必須將其轉換爲具有HTML標籤並在webview中顯示的表單。我所擁有的編碼數據是這種格式: -這是什麼類型的編碼?我應該用什麼腳本來解碼?

有+ a + pet + + +很像+養育子女%2C + you + just + can + + stand + the + chance + being + away + from +其中,+仍然%2C +十分+往往+它+出現+是+您+不能+擁有+你的+寵物+左右+到處+那麼+你+需要+到+住房+他們+別處+爲A +,而+但是+在+的+一天%的+端+ 2C +一個+一定+承認+是+它是+漂亮+硬盤+到+信託+人+有+我們+毛茸茸+嬰兒。+誰+知道+他們+比+我們%2C更好+ +的+習慣%2C +飲食+單%2C +氣質+和+社會+傾向%3F%0D%0A%0D%0AIt +是+,因爲+如此+ +非常+ +的原因是我們+ +在+%3CA + HREF%3D%22https%3A%2F%2Fwww.whatsuplife.in%2Fnoida%+ 22%的目標三維%22_blank%22 +相對%3D%22noopener%22%3EWhat %92S +上+諾伊達+是+與共享%3C%2FA%3E + +您+的+ 5個+寵物+寄宿+設備+這+你+可以+實際上+信任。%0D%0A%的+目錄+ 3CH 4%3E%3Cstrong%3EThe +狗+度假村%2C +諾依+%3C%2Fstrong%3E%3C%2Fh4%3E%0D%0AAt +噸他+狗+酒店+諾伊達%2C +被+保證+是+你的+寵物+會+被+關心+由+透徹+專業+。+%A0They +度假+擁有+的+ A + 10%2C000 +平方+英尺+房屋+區域+地方+約+ 5000 +平方+ .ft +是+寬+開放+綠色+區域+到+使+你的+寵物+住宿+俏皮+和+歡呼。+在+最好+縱橫+約+在+地點+是+是+有是+完整+醫療+保健+和+行爲+逮捕的+寵物+ +爲+它們+擁有+獸+醫生+ A +專用+團隊+ %2C +狗+訓練者%2C +和+其它+寵物+護理+專業知識。%0D 0A%%0D 0A%%3CA + HREF%3D%22https%3A%2F%2Fwww.whatsuplife.in%2Fnoida%2Fblog% 2Fwp含量%2Fuploads%2F2017%2F06%2FDog-度假noida.png%22%3E%3Cimg +類%3D%22aligncenter + WP-圖像-4902 +大小滿%22 + SRC%3D%22https%3A% 2F%2Fwww.whatsuplife.in%2Fnoida%2Fblog%2Fwp含量%2Fuploads%2F2017%2F06%2FDog-度假noida.png%22 + ALT%3D%22Dog +景區+諾依%22 +寬度%3D%221052% 22 +高度%3D%22326%22 +%2F%3E%3C%2FA%3E%0D%0A%0D%0A%3Cstrong%3EAddress%3A%A0 +部門+ 112%2C +諾依%3C%2Fstrong%3E %0D 0A%%3Cstrong%3E +聯繫%3A + 9911782646%3C%2Fstrong%3E%0D 0A%%3CH 4%3E%3Cstrong%3EB EST +寵物+登機+%3C%2Fstrong%3E%3C%2Fh4%3E%0D%0AThey +是+確實+一+ +中+高+評價+和+積極+評論+在+寵物+寄宿+設備+諾伊達+不+只是+爲+狗+但是+它們+擁有+ A +友善+普羅維登斯+寵物+護理+的和+其他+寵物+喜歡+魚%2C +鳥+ +日+登機+服務+ %2C +豚鼠+豬%2C +倉鼠%2C +貓%2C +和+其他+爲+以及%A0 +創造+的+想法是+%91no籠%92 +免費+爲+空間+心愛+寵物+中+通過+卡佛力+蛙+巴德瓦傑+和+ Yashraj +巴德瓦傑開發+ +在+ +最佳+寵物+寄宿%2C +在+重點是+上+製作+你的+寵物+社交+和+擁有+ A +免費+會+遊蕩在+寬+空間+的。+他們+相信+ IN + + A +訓練有素+狗+這+說+出租+的+獨特+概念+ +到+是+自己+%91alpha%92 + +其他+寵物+狗%2C之間+本+維護+在+禮儀+所有+的+時間。%0D%0A%0D%0A%3CA + HREF%3D%22https %3A%2F%2Fwww.whatsuplife.in%2Fnoida%2Fblog%2Fwp含量%2Fuploads%2F2017%2F06%2Fbest-PET-boarding.jpg%22%3E%3Cimg +類%3D%22aligncenter + WP-圖像4903 +大小滿%22 + SRC%3D%22https%3A%2F%2Fwww.whatsuplife.in%2Fnoida%2Fblog%2Fwp - 共ntent%2Fuploads%2F2017%2F06%2Fbest-PET-boarding.jpg%22 + ALT%3D%22best +寵物+登機+諾依%22 +寬度%3D%22960%22 +高度%3D%22489 22%+%2F %3E%3C%2FA%3E%0D%0A%0D%0A%3Cstrong%3EAddress%3A + B + 14 + 1 +奧米克+ 2%2C +大+諾依%3C%2Fstrong%3E%0D%0A%3Cstrong %3E +電話%3A + 09711951179%3C%2Fstrong%3E%0D%0A%3CH 4%3E%3Cstrong%3EPet +首頁+事務+節+登機+%3C%2Fstrong%3E%3C%2Fh4%3E%0D%0A %3CA +風格%3D%22font尺寸%3A + 14px的%3B +字體家庭%3A +%27Open +三世%27%2C + Arial字體%2C +無襯線%3B +彩色%3A +%2319232d%3B +文本 - 裝飾線%3A +下劃線%3B%22 + HREF%3D%22https%3A%2F%2Fwww.whatsuplife.in%2Fnoida%2Fblog%2Fwp含量%2Fuploads%2F2017%2F06%2Fpet-家庭事務日-care.jpg%22%3E%3Cimg +類%3D%22alignright + WP-圖像-4906 +大小滿%22 +風格%3D%22font尺寸%3A + 14px的%3B%22 + SRC%3D%22https %3A%2F%2Fwww.whatsuplife.in%2Fnoida%2Fblog%2Fwp含量%2Fuploads%2F2017%2F06%2Fpet-家庭事務天care.jpg%22 + ALT%3D%22pet +登機+在+諾伊達%22 +寬度%3D%22350%22 +高度%3D%22263 22%+%2F%3E%3C%2FA%3E%0D 0A%%0D%0APet +首頁+事務+享有+ A + ST二苓+口碑+在+寵物+護理+行業+和是+一個++的+卓越+服務+在+諾伊達。+在+ +登機%2C +有+是+專業+誰+理解+的+需要+你的+寵物+ +和+也+你的+優惠+約+在+專用+服務+你+需要+爲你+寵物+寵物+護理+系統+中+以及+計劃+爲+餐+和+醫學+計時+和+下+在+護理+寵物+護理+ +專家+這裏。%0D%0A%0D%0A%3Cstrong%3EAddress%3A%A0 + SUPERTECH +開普敦%2C +行業+ 74%2C +諾依。%3C%2Fstrong%3E%0D%0A%3Cstrong%3E +電話%3A + 09873204636%3C%2Fstrong%3E%0D%0A%3CH 4%3E%3Cstrong%3EClean +可愛+%3C% 2Fstrong%3E%3C%2Fh4%3E%0D%0AWith +的+ 10%2C000 +平方+ FT +狗+寬+空格+ +日+護理+和+寵物+度假+家庭%2C +清潔+可愛+ +是衆所周知+到+服務+全尺寸+%2C +各類%2C +品種%2C +和+寵物+年齡+。+他們+是+最大的+和+最好的寄宿+工廠+不僅沒有+在+諾伊達+而且+也+在+ NCR +清潔+可愛+中+管理+通過+ A +高度+技能+狗+處理+團隊+產品+寵物+修飾+服務%2C +休閒+活動%2C +健康+關心%2C +療法%2C +寵物+育兒輔導%2C +狗+行爲+管理%2C +護膚+護理% 2C +狗+康復+和+其實%2C +什麼不是。0D%0A%0D%0A%3CA%+ HREF%3D%22https%3A%2F%2Fwww.whatsuplife.in%2Fnoida%2Fblog%2Fwp內容%2Fuploads%2F2017%2F06%2FBoarding-大File.jpg%22%3E%3Cimg +類%3D%22aligncenter + WP-圖像-4907 +尺寸大22%+的src%3D%22https%3A%2F%2Fwww .whatsuplife.in%2Fnoida%2Fblog%2Fwp含量%2Fuploads%2F2017%2F06%2FBoarding-大文件的1024x682.jpg%22 + ALT%3D%22dog +登機+在+諾依%22 +寬度%3D%22702 %22 +高度%3D%22468%22 +%2F%3E%3C%2FA%3E%0D%0A%0D%0A%26nbsp%3B%0D%0A%0D%0A%3Cstrong%3EAddress%3A%A0 +扇區+ 112%2C +諾依%3C%2Fstrong%3E%0D%0A%3Cstrong%3E +電話%3A + 08076548163%3C%2Fstrong%3E%0D%0A%3CH 4%3E%3Cstrong%3EGoDogee + PET +服務+% 3C%2Fstrong%3E%3C%2Fh4%3E%0D%0AGoDogee是+一個+ O F +在+印度%92S +龍頭+寵物+護理+服務+提供+寵物+日+護理%2C +寵物+酒店%2C +寵物+美容%2C +寵物+接種%2C +寵物+治療%2C + PET +匹配+和+其他+有趣+和+獨特+ +服務的排序+%0D%0A%0D%0A%3Cimg +類%3D%22aligncenter +尺寸全+ WP-圖像4905%22 + SRC %3D%22https%3A%2F%2Fwww.whatsuplife.in%2Fnoida%2Fblog%2Fwp含量%2Fuploads%2F2017%2F06%2Fgodogee.jpg%22 + ALT%3D%22%22 +寬度%3D%221920 22% +高度%3D%22790%22 +%2F%3E%0D%0A%0D%0A%3Cstrong%3EAddress%3A + J-10%2C +諾依+路%2C + H +塊%2C +部門+ 8% 2C +諾伊達%3C%2Fstrong%3E%0D%0A%3Cstrong%3E +電話%3A + 09910664343%3C%2Fstrong%3E

什麼樣的格式是這樣的,我如何將其轉換成文本和HTML標籤?

我目前使用這種方法,但我得到一個無返回值:

func urlDecode(_ htmlString: String) -> String { 

    var decodedURL = htmlString.replacingOccurrences(of: "+", with: " ") 
    decodedURL = decodedURL.removingPercentEncoding! 
    return decodedURL 
} 
+0

這是基本的URL編碼... – CBroe

+0

@shallowThought - 回滾你的編輯,明白你爲什麼這樣做,但不幸的是刪除線索的過程中的問題。 – CRD

+0

@CRD好的。抱歉。我以爲我小心留下某些'%0A's。別擔心。 – shallowThought

回答

0

所以你得到nil,這意味着你的字符串中有一些無效的東西。我們怎麼能找出什麼?那麼它肯定不是空間,所以讓我們打破你的字符串轉換成「字」:

var decodedURL = htmlString.replacingOccurrences(of: "+", with: " ") 
let chunks = decodedURL.components(separatedBy: " ") 

現在嘗試解碼每個「單詞」,看看哪些失敗:

for chunk in chunks 
{ 
    let decoded = chunk.removingPercentEncoding 
    if decoded == nil 
    { 
     print("\(chunk)") 
    } 
} 

此輸出:

rel%3D%22noopener%22%3EWhat%92s 
%A0They 
%2F%3E%3C%2Fa%3E%0D%0A%0D%0A%3Cstrong%3EAddress%3A%A0 
well.%A0 
%91no-cage%92 
%91alpha%92 
here.%0D%0A%0D%0A%3Cstrong%3EAddress%3A%A0 
%2F%3E%3C%2Fa%3E%0D%0A%0D%0A%26nbsp%3B%0D%0A%0D%0A%3Cstrong%3EAddress%3A%A0 
India%92s 

現在removingPercentEncoding解碼UTF-8,所以檢查每個那些%序列以查看它們是否是有效的UTF-8示出了91%,92%%& A0不是。但是,它們是有效的Windows-1252編碼 - 單個捲曲引號和非破壞空間。

所以你需要解碼字符串爲Windows-1252而不是UTF-8。你有時間查看文檔...

HTH

+0

非常感謝!自從最近3個小時以來,我一直在打我的腦袋。我從API獲得的其他回覆是UTF-8編碼,所以我推測這也是山姆會員。我將嘗試將字符串解碼爲Windows - 1252,歡呼! –

+0

@AkshayYerneni:將輸入減少爲*最小*示例顯示問題實際上是*您的*部分,*之前*發佈問題。 –

0

從蘋果,

一個新的字符串的百分比編碼序列中移出,或零如果 接收器包含無效的編碼百分比序列。只能在您知道編碼百分比的字符串上調用 此方法。 在非百分比編碼的字符串上調用此方法會導致 將百分比字符誤解爲 百分比編碼序列的開頭。

你得到零,因爲removingPercentEncoding不能識別你的一些百分比編碼。我將它粘貼到online parser上,發現%A0和%92似乎是可疑的。