0

What I want to achieve is I want to clean all in line CSS text (Style but not HTML) from a block of text example:

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

And I found an extension from other question that they can delete an HTML tag from a text and this is the extension

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

So how can we delete a CSS inline text from HTML string?

The result it's should be look like this, there's no more style in html string

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

Tested String:

    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>
"""
3
  • can provide an expected result ? Commented Sep 18, 2018 at 9:23
  • @Tobi I just updated the result that's should be done Commented Sep 18, 2018 at 9:59
  • first you need the string in the right format, from what i see the string is not correct considering you got the string right ill try to give you a solution ;) Commented Sep 18, 2018 at 10:15

1 Answer 1

1

UPDATED,

since you have new update that you have the html formatted as string correctly you can try this code,

using regex

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: "")
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for mention that library. but that's not what I want! I need to remove CSS not HTML though
use it to convert html to swift string, then filter the string to remove the tag simple :) @SopheakSok
actually maybe you don't understand my question, the HTML that I show you are already a String text that gets from an API and I can use attributestring to Show the text inside Textview with existing function. but those HTML came with CSS and it depends on how that CSS define. so in android, I can use TextJustify-Android Library to avoid all those CSS texts so each string that gets from API will look the same even the CSS define it as different. anw thank you for your time
i tried to copy paste your string , in swift its not valid string
i've tried to filter it natively on swift, but i couldnt have a valid string if u provide on i will probably solve the issue
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.