如何从swift中的字符串中删除所有html

时间:2014-10-24 22:42:01

标签: html swift

考虑这个字符串值:

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 />

如何在Swift中删除所有html标签?

所以结果必须是:

LCD Soundsystem was the musical project of producer James Murphy, co-founder of dance-punk label DFA 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 alternative dance and post punk, along with elements of disco and other styles. 

4 个答案:

答案 0 :(得分:4)

你可以使用正则表达式,注意我创建的那个:

    var str = "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 regex:NSRegularExpression  = NSRegularExpression(
        pattern: "<.*?>",
        options: NSRegularExpressionOptions.CaseInsensitive,
        error: nil)!


    let range = NSMakeRange(0, countElements(str))
    let htmlLessString :String = regex.stringByReplacingMatchesInString(str,
        options: NSMatchingOptions.allZeros,
        range:range ,
        withTemplate: "")


    println(htmlLessString)

它转换:

"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 />"

"LCD Soundsystem was the musical project of producer James Murphy, co-founder of dance-punk label DFA 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 alternative dance and post punk, along with elements of disco and other styles."

唯一的问题是我已将所有双引号(“)转换为单引号然后应用正则表达式,否则我需要使用"\"

将它们全部转义

<强>更新

我还尝试使用"\"转义所有双引号,结果仍然相同:

我使用的新字符串是:

"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 />"

和结果:

"LCD Soundsystem was the musical project of producer James Murphy, co-founder of dance-punk label DFA 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 alternative dance and post punk, along with elements of disco and other styles."

答案 1 :(得分:3)

这是为Swift 2.0重写的CjCoaxs代码:

var str = "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 regex = try! NSRegularExpression(pattern: "<.*?>", options: [.CaseInsensitive])

let range = NSMakeRange(0, input.characters.count)
let htmlLessString :String = regex.stringByReplacingMatchesInString(input, options: [],
    range:range ,
    withTemplate: "")

print(htmlLessString)

答案 2 :(得分:1)

以下是Swift 3.0的代码:

do {
        let regex =  "<[^>]+>"
        let expr = try NSRegularExpression(pattern: regex, options: NSRegularExpression.Options.caseInsensitive)
        let replacement = expr.stringByReplacingMatches(in: originalString, options: [], range: NSMakeRange(0, comment.characters.count), withTemplate: "")
        //replacement is the result
    } catch {
        // regex was bad!
    }

答案 3 :(得分:0)

试试SwiftSoup这很简单

do{
    let html = "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 doc: Document = try SwiftSoup.parse(html)
    return try doc.text()
}catch Exception.Error(let type, let message)
{
    print("")
}catch{
    print("")
}