[英]How to separate HTML string into array or dictionary by Swift3?
我從API獲得HTML字符串是這樣的:
let a: String = "<a href="https://www.google.com.tw">https://www.google.com.tw </a>"
let b: String = "<a href="myAppName://app/user/aa3b77411825b88b318d77gg">@Tim </a>Hello Tim"
let c: String = "<a href="myAppName://app/user/aa3b77411825b88b318d77gg">@Tim </a><a href="https://www.google.com.tw">https://www.google.com.tw </a>"
let splitedArray1: [String] = a.componentsSeparatedByString("?????") //splited string which is the best
let splitedArray2: [String] = b.componentsSeparatedByString("?????") //splited string which is the best
let splitedArray3: [String] = c.componentsSeparatedByString("?????") //splited string which is the best
我想從他們分離鏈接,並獲得如下數據
print(splitedArray1) //["https://www.google.com.tw","https://www.google.com.tw"]
print(splitedArray2) //["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","Hello Tim"]
print(splitedArray3) //["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","https://www.google.com.tw","https://www.google.com.tw "]
可能的解決方案:使用NSAttributedString
然后在NSLinkAttributeName
上枚舉,如果沒有,則意味着沒有鏈接標記,因此您只需保留“字符串”,否則,添加鏈接,然后添加字符串。
快速在Playground中編寫:
let a: String = "<a href=\"https://www.google.com.tw\">https://www.google.com.tw </a>"
let b: String = "<a href=\"myAppName://app/user/aa3b77411825b88b318d77gg\">@Tim </a>Hello Tim"
let c: String = "<a href=\"myAppName://app/user/aa3b77411825b88b318d77gg\">@Tim </a><a href=\"https://www.google.com.tw\">https://www.google.com.tw </a>"
let values:[String] = [a, b, c]
for aHTMLString in values
{
let attributedString = try! NSAttributedString.init(data: aHTMLString.data(using: .utf8)!,
options: [.documentType: NSAttributedString.DocumentType.html],
documentAttributes: nil)
var retValues = [String]()
attributedString.enumerateAttribute(.link,
in: NSRange(location: 0, length: attributedString.string.count),
options: [],
using: { (attribute, range, pointerStop) in
if let attribute = attribute as? URL
{
retValues.append(attribute.absoluteString)
}
let subString = (attributedString.string as NSString).substring(with: range)
retValues.append(subString)
})
print("*** retValues: \(retValues)")
}
let targetResult1 = ["https://www.google.com.tw","https://www.google.com.tw"]
let targetResult2 = ["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","Hello Tim"]
let targetResult3 = ["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","https://www.google.com.tw","https://www.google.com.tw "]
print("targetResult1: \(targetResult1)")
print("targetResult2: \(targetResult2)")
print("targetResult3: \(targetResult3)")
輸出:
*** retValues: ["https://www.google.com.tw/", "https://www.google.com.tw "]
*** retValues: ["myappname://app/user/aa3b77411825b88b318d77gg", "@Tim ", "Hello Tim"]
*** retValues: ["myappname://app/user/aa3b77411825b88b318d77gg", "@Tim ", "https://www.google.com.tw/", "https://www.google.com.tw "]
targetResult1: ["https://www.google.com.tw", "https://www.google.com.tw"]
targetResult2: ["myAppName://app/user/aa3b77411825b88b318d77gg", "@Tim ", "Hello Tim"]
targetResult3: ["myAppName://app/user/aa3b77411825b88b318d77gg", "@Tim ", "https://www.google.com.tw", "https://www.google.com.tw "]
有一些細微的差別,我復制了您的“目標”(splitArray),並且在最后一個中缺少空格,並且我的代碼傾向於在鏈接上添加最后的“ /”。
我已經創建了此擴展名以獲取URL。
extension String {
func getUrl() -> String? {
let rss = self.split { (char) -> Bool in
return char == ">"
}
if let final = rss.last?.split(separator: "<"), let first = final.first {
return String(first)
}
return nil
}
var hrefUrl: String {
let matchString = "=\""
let arrComponents = self.components(separatedBy: matchString)
if let first = arrComponents.last, let str = first.split(separator: "\"").first {
return String(str)
}
return ""
}
}
用法:
let a: String = "<a href=\"https://www.google.com.tw\">https://www.google.com.tw </a>"
a.getUrl() //output: https://www.google.com.tw
//or
a.hrefUrl //output: https://www.google.com.tw
無需庫的簡單解決方案-只需使用String.replaceOccurences(of:...即可替換諸如href的奇數字符串,將其拆分為拆分參數(例如“ |”),然后使用componentsSeparatedByString(“ |”)來獲取組件。
使用正則表達式提取URL。 下面我寫了代碼片段。
let text = "<a href=\"https://www.google.com\">"
let regex = try! NSRegularExpression(pattern: "<a[^>]+href=\"(.*?)\"[^>]*>")
let range = NSMakeRange(0, text.characters.count)
let matches = regex.matches(in: text, range: range)
for match in matches {
let strURL = (text as NSString).substring(with: match.rangeAt(1))
print(strURL)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.