【问题标题】:Removing everything between a certain set of characters with Swift使用 Swift 删除特定字符集之间的所有内容
【发布时间】:2014-12-26 20:36:40
【问题描述】:

我对 Swift 和原生编程很陌生,对于我自己做的一个小项目,我在 twitter 搜索后得到了完整的 html,我试图过滤掉文本的第一条推文。我的重点是我能够获得第一条推文,包括其中的所有标签,但我对如何过滤掉那里的文本并删除 HTML 元素有点无能为力。

例如,获取一条推文并过滤掉可能的<a href=""><span> 等非常容易。但是当我更改推文或搜索时,它就不会那么具体了。我真正要寻找的是如何删除以结尾的字符串中的所有内容。这样,我就可以过滤掉字符串中不需要的所有内容。我正在使用“string.componentsSeparatedByString()”从所有 HTML 中获取我需要的一条推文,但我不能使用这种方法从我的字符串中过滤出所有内容。

请多多包涵,因为我在这方面还很陌生,我知道我什至可能根本没有正确地做这件事,而且有一种更简单的方法可以拉出一条推文,而不是所有这些麻烦。如果是这样,也请告诉我。

【问题讨论】:

    标签: string swift


    【解决方案1】:

    您可以创建一个函数来为您执行以下操作:

    func html2String(html:String) -> String {
        return NSAttributedString(data: html.dataUsingEncoding(NSUTF8StringEncoding)!, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
    }
    

    或作为扩展:

    extension String {
        var html2String:String {
            return NSAttributedString(data: dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
        }
        var html2NSAttributedString:NSAttributedString {
            return NSAttributedString(data: dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!
        }
    }
    

    你可能更喜欢作为 NSData 扩展

    extension NSData{
        var htmlString:String {
            return  NSAttributedString(data: self, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
        }
    }
    

    或 NSData 作为函数:

    func html2String(html:NSData)-> String {
        return  NSAttributedString(data: html, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
    }
    

    用法:

    "<div>Testing<br></div><a href=\"http://stackoverflow.com/questions/27661722/removing-everything-between-a-certain-set-of-characters-with-swift/27662573#27662573\"><span>&nbsp;Hello World !!!</span>".html2String  //  "Testing\n Hello World !!!"
    
    let result = html2String("<div>Testing<br></div><a href=\"http://stackoverflow.com/questions/27661722/removing-everything-between-a-certain-set-of-characters-with-swift/27662573#27662573\"><span>&nbsp;Hello World !!!</span>")  //  "Testing\n Hello World !!!"
    

    // 让这个html作为字符串加载

    import UIKit
    
    class ViewController: UIViewController {
        let questionLink = "http://stackoverflow.com/questions/27661722/removing-everything-between-a-certain-set-of-characters-with-swift/27662573#27662573"
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            if let questionUrl = NSURL(string: questionLink) {
                println("LOADING URL")
                if let myHtmlDataFromUrl = NSData(contentsOfURL: questionUrl){
                    println(myHtmlDataFromUrl.htmlString)
                }
            }
        }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    

    【讨论】:

    • 好的,有没有办法保留大部分标签,只需取出某个标签,例如
    【解决方案2】:

    在过去几年中,Swift 中的很多价值观发生了变化,所以我只想发布 Leo Dabus 答案的更新版本,更新为当前的 Swift 语法。

    extension String {
    
        func removeHTMLEncoding() throws -> String? {
            guard let data = self.data(using: .utf8) else { return nil }
            let attr = try NSAttributedString(
                data: data,
                options: [
                    .documentType: NSAttributedString.DocumentType.html,
                    .characterEncoding: NSNumber(value: String.Encoding.utf8.rawValue)
                ],
                documentAttributes: nil
            )
            return attr.string
        }
    
    }
    

    您仍然需要将字符串编码值转换为 NSNumber 有点烦人 - NSAttributedString 已经过时了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-22
      • 1970-01-01
      • 2018-07-15
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      相关资源
      最近更新 更多