gpt4 book ai didi

ios - 如何在 Swift4 中从 HTML 字符串中清除 CSS 文本

转载 作者:行者123 更新时间:2023-11-28 14:13:03 24 4
gpt4 key购买 nike

我想要实现的是我想从一段文本示例中清除所有内联 CSS 文本(样式而不是 HTML):

<p style="\&quot;text-align:" justify;="" \"=""><span style="\&quot;font-size:" 13px;="" font-family:="" arial;="" text-decoration-skip-ink:="" none;\"=""><b><span style="font-size: 18px;">Angkor Wat</span></b> is a temple complex in Cambodia and the largest religious monument in the world, on a site measuring 162.6 hectares (1,626,000 m2; 402 acres). It was originally constructed as a Hindu temple dedicated to the god Vishnu for the Khmer Empire, gradually transforming into a Buddhist temple towards the end of the 12th century. It was built by the Khmer King Suryavarman II in the early 12th century in Yaśodharapura, the capital of the Khmer Empire, as his state temple and eventual mausoleum. Breaking from the Shaiva tradition of previous kings, Angkor Wat was instead dedicated to Vishnu. As the best-preserved temple at the site, it is the only one to have remained a significant religious centre since its foundation. The temple is at the top of the high classical style of Khmer architecture. It has become a symbol of Cambodia, appearing on its national flag, and it is the country\'s prime attraction for visitors.</span></p>

我从其他问题中找到了一个扩展,他们可以从文本中删除 HTML 标签,这就是扩展

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

那么我们如何从 HTML 字符串中删除 CSS 内联文本呢?

结果应该是这样的,html字符串中没有更多的样式

<p><span><b><span>Angkor Wat</span></b> is a temple complex in Cambodia and the largest religious monument in the world, on a site measuring 162.6 hectares (1,626,000 m2; 402 acres). It was originally constructed as a Hindu temple dedicated to the god Vishnu for the Khmer Empire, gradually transforming into a Buddhist temple towards the end of the 12th century. It was built by the Khmer King Suryavarman II in the early 12th century in Yaśodharapura, the capital of the Khmer Empire, as his state temple and eventual mausoleum. Breaking from the Shaiva tradition of previous kings, Angkor Wat was instead dedicated to Vishnu. As the best-preserved temple at the site, it is the only one to have remained a significant religious centre since its foundation. The temple is at the top of the high classical style of Khmer architecture. It has become a symbol of Cambodia, appearing on its national flag, and it is the country\'s prime attraction for visitors.</span></p>

测试字符串:

    let html = """
<p style="text-align: justify; "><span style="font-size: 13px; font-family: Arial; text-decoration-skip-ink: none;"><b>The Bayon</b> is a well-known and richly decorated Khmer temple at Angkor in Cambodia. Built in the late 12th or early 13th century as the official state temple of the Mahayana Buddhist King Jayavarman VII (ព្រះបាទជ័យវរ្ម័នទី ៧), the Bayon stands at the centre of Jayavarman's capital, Angkor Thom (អង្គរធំ).Following Jayavarman's death, it was modified and augmented by later Hindu and Theravada Buddhist kings in accordance with their own religious preferences.</span></p><p style="text-align: justify; "><span style="font-size: 13px; font-family: Arial; text-decoration-skip-ink: none;">The Bayon's most distinctive feature is the multitude of serene and smiling stone faces on the many towers which jut out from the upper terrace and cluster around its central peak. The temple is known also for two impressive sets of bas-reliefs, which present an unusual combination of mythological, historical, and mundane scenes. The current main conservatory body, the Japanese Government Team for the Safeguarding of Angkor (the JSA) has described the temple as "the most striking expression of the baroque style" of Khmer architecture, as contrasted with the classical style of Angkor Wat (ប្រាសាទអង្គរវត្ត).</span></p>
"""

最佳答案

已更新,

因为你有新的更新,你有正确格式化为字符串的 html 你可以试试这个代码,

使用正则表达式

let regex = try! NSRegularExpression(pattern: "style=\"([^\"]*)\"", options: .caseInsensitive)
let range = NSMakeRange(0, html.characters.count)
let modString = regex.stringByReplacingMatches(in: html, options: [], range: range, withTemplate: "")

关于ios - 如何在 Swift4 中从 HTML 字符串中清除 CSS 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52377436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com