Eliminazione di tag HTML da una stringa


95

Come rimuovo i tag HTML da una stringa in modo da poter produrre testo pulito?

let str = string.stringByReplacingOccurrencesOfString("<[^>]+>", withString: "", options: .RegularExpressionSearch, range: nil)
print(str)


1
Led, questa domanda ha molto valore ma così com'è, rischia di chiudersi perché non fai una domanda chiara: è uno scenario non riproducibile. Ti suggerisco di riformulare la tua domanda come da Come chiedere . Non vorrei che quella domanda venisse cancellata.
Tunaki

3
lol stackoverflow ... come viene chiuso come "fuori tema"? È il risultato di Google numero 1 per "Swift remove html tags".
canhazbits

2
@canhazbits lo so bene! Fare clic su riapri per designare la riapertura.
Led

1
Swift 3: string.replacingOccurrences (di: "<[^>] +>", con: "", opzioni: .regularExpression, intervallo: nil)
etayluz

Risposte:


147

Hmm, ho provato la tua funzione e ha funzionato su un piccolo esempio:

var string = "<!DOCTYPE html> <html> <body> <h1>My First Heading</h1> <p>My first paragraph.</p> </body> </html>"
let str = string.stringByReplacingOccurrencesOfString("<[^>]+>", withString: "", options: .RegularExpressionSearch, range: nil)
print(str)

//output "  My First Heading My first paragraph. "

Puoi fare un esempio di problema?

Versione Swift 4 e 5:

var string = "<!DOCTYPE html> <html> <body> <h1>My First Heading</h1> <p>My first paragraph.</p> </body> </html>"
let str = string.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)

25
<LOL> Ah ah! </LOL>
Steve Rosenberg,


1
Ad esempio, prova questo pezzo di HTML:<p foo=">now what?">Paragraph</p>
The Paramagnetic Croissant

32
In Swift 3 string.replacingOccurrences(of: "<[^>]+>", with: "", options: String.CompareOptions.regularExpression, range: nil)
Husam

5
In Swift 4 string.replacingOccurrences (di: "<[^>] +>", con: "", opzioni: .regularExpression, intervallo: nil)
Raegtime

29

Poiché HTML non è un linguaggio normale (HTML è un linguaggio privo di contesto ), non è possibile utilizzare le espressioni regolari. Vedere: Utilizzo di espressioni regolari per analizzare l'HTML: perché no?

Considererei invece l'utilizzo di NSAttributedString.

let htmlString = "LCD Soundsystem was the musical project of producer <a href='http://www.last.fm/music/James+Murphy' class='bbcode_artist'>James Murphy</a>, co-founder of <a href='http://www.last.fm/tag/dance-punk' class='bbcode_tag' rel='tag'>dance-punk</a> label <a href='http://www.last.fm/label/DFA' class='bbcode_label'>DFA</a> Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of <a href='http://www.last.fm/tag/alternative%20dance' class='bbcode_tag' rel='tag'>alternative dance</a> and <a href='http://www.last.fm/tag/post%20punk' class='bbcode_tag' rel='tag'>post punk</a>, along with elements of <a href='http://www.last.fm/tag/disco' class='bbcode_tag' rel='tag'>disco</a> and other styles. <br />"    
let htmlStringData = htmlString.dataUsingEncoding(NSUTF8StringEncoding)!
let options: [String: AnyObject] = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding]
let attributedHTMLString = try! NSAttributedString(data: htmlStringData, options: options, documentAttributes: nil)
let string = attributedHTMLString.string

Oppure, come farebbe Irshad Mohamed nei commenti:

let attributed = try NSAttributedString(data: htmlString.data(using: .unicode)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
print(attributed.string)

7
Questo sembra essere l'approccio più pulito e funziona meravigliosamente bene! È meglio lasciare che il framework Foundation, testato in battaglia, gestisca questo per te, invece di scrivere parser traballanti da solo.
Shyam Bhat

4
Pulito!! let attributed = try NSAttributedString(data: htmlString.data(using: .unicode)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) print(attributed.string)la maggior parte delle persone preferisce scegliere risposte piccole e facili da capire.
Irshad Mohamed

1
Grazie per la soluzione! È possibile salvare gli spazi e le interruzioni di riga mentre rimuoviamo i tag html? Attualmente, tutte le interruzioni di riga vengono ignorate nella nuova stringa.
Astha Gupta

7
Solo un avvertimento usando questo: conversione (attribuzione) dello stile HTML lenta! . Un ingegnere CoreText al WWDC mi ha detto che questo non è più mantenuto e se ne era completamente dimenticato.
Sirene

1
Solo un avvertimento sull'avvertimento precedente: vediamo alcuni dati prima di scartare un metodo perché troppo "lento". Ci sono molte librerie C che usi (spesso senza rendertene conto) che non richiedono molta manutenzione. Non è necessariamente una cosa negativa.
Joony

10

Soluzione Mohamed ma come estensione String in Swift 4.

extension String {

    func stripOutHtml() -> String? {
        do {
            guard let data = self.data(using: .unicode) else {
                return nil
            }
            let attributed = try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
            return attributed.string
        } catch {
            return nil
        }
    }
}

8

Sto utilizzando la seguente estensione per rimuovere elementi HTML specifici:

extension String {
    func deleteHTMLTag(tag:String) -> String {
        return self.stringByReplacingOccurrencesOfString("(?i)</?\(tag)\\b[^<]*>", withString: "", options: .RegularExpressionSearch, range: nil)
    }

    func deleteHTMLTags(tags:[String]) -> String {
        var mutableString = self
        for tag in tags {
            mutableString = mutableString.deleteHTMLTag(tag)
        }
        return mutableString
    }
}

Ciò rende possibile rimuovere solo i <a>tag da una stringa, ad esempio:

let string = "my html <a href="">link text</a>"
let withoutHTMLString = string.deleteHTMLTag("a") // Will be "my  html link text"

@Mr Lister c'è un modo per rimuovere tutti i tag html e mantenere questo <a href=""> testo del collegamento </a>?
Mazen Kasser

6
extension String{
    var htmlStripped : String{
        return self.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
    }
}

Codifica felice


3

swift 4:

extension String {
    func deleteHTMLTag(tag:String) -> String {
        return self.replacingOccurrences(of: "(?i)</?\(tag)\\b[^<]*>", with: "", options: .regularExpression, range: nil)
    }

    func deleteHTMLTags(tags:[String]) -> String {
        var mutableString = self
        for tag in tags {
            mutableString = mutableString.deleteHTMLTag(tag: tag)
        }
        return mutableString
    }
}

2
oppure puoi usare in questo modo: func deleteHTMLTag () -> String {return self.replacingOccurrences (of: "(? i) </? \\ b [^ <] *>", con: "", opzioni: .regularExpression , range: nil)}
Anil Kumar

Questa regex non rimuove il codice html per me. Stringa di esempio: "<b> Ai gatti </b> piace fare qualcosa". Non ho indagato di più sul motivo per cui non funziona. Ma text.replacingOccurrences (of: "<[^>] +>", ....) funziona per i miei casi semplici.
Benjamin Piette

2

Aggiornato per Swift 4:

guard let htmlStringData = htmlString.data(using: .unicode) else { fatalError() }

let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
                .documentType: NSAttributedString.DocumentType.html
                .characterEncoding: String.Encoding.unicode.rawValue
             ]

let attributedHTMLString = try! NSAttributedString(data: htmlStringData, options: options, documentAttributes: nil)
let string = attributedHTMLString.string

ti manca un "," dopo .documentType: param
cwgso

0

Preferisco usare un'espressione regolare piuttosto che usare la conversione HTML di NSAttributedString, tieni presente che richiede molto tempo e deve essere eseguito anche sul thread principale. Maggiori informazioni qui: https://developer.apple.com/documentation/foundation/nsattributedstring/1524613-initwithdata

Per me questo ha funzionato, prima rimuovo qualsiasi stile in linea CSS e poi tutti i tag HTML. Probabilmente non solido come l'opzione NSAttributedString, ma molto più veloce per il mio caso.

extension String {
    func withoutHtmlTags() -> String {
        let str = self.replacingOccurrences(of: "<style>[^>]+</style>", with: "", options: .regularExpression, range: nil)
        return str.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
    }
}
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.