Forum Discussion

cpradio's avatar
cpradio
Visitor
15 years ago

Strip out HTML Tags

I am reading an XML file to populate a springboard, but the descriptions in the XML have HTML tags embeded in them. How can I quickly strip these out?

Thanks,
Matt

6 Replies

  • For anyone that's interested I used this...

    function StringRemoveHTMLTags(baseStr as String) as String
    r = createObject("roRegex", "<[^<]+?>", "i")
    return r.replaceAll(baseStr, "")
    end function
  • "lewi-p" wrote:
    For anyone that's interested I used this...

    function StringRemoveHTMLTags(baseStr as String) as String
       r = createObject("roRegex", "<[^<]+?>", "i")
       return r.replaceAll(baseStr, "")
    end function


    Thank you 🙂  Does exactly what it says.  I figured I'd say thanks 5yrs later since nobody else did
  • "cpradio" wrote:
    I am reading an XML file to populate a springboard, but the descriptions in the XML have HTML tags embeded in them. How can I quickly strip these out?


    Do a google search for a regular expression to strip HTML (there's tons online), and use the regular expression component in brightscript to remove them.
  • Ah, missed the regular expression component. Thanks, that should do the trick. Was kinda hoping it would support the HTML tags, but I understand why it doesn't.
  • jbrave's avatar
    jbrave
    Channel Surfer
    I've had way more success using string functions like instr, mid left and right than regex. I posted a regex I found for parsing HTML a while ago, but have never actually gotten it to work beyond giving me the string "<HTML>" so if anyone has some code that works to get a specific tag or an associativearray of tags it would be awesome.
  • "jbrave" wrote:
    I've had way more success using string functions like instr, mid left and right than regex. I posted a regex I found for parsing HTML a while ago, but have never actually gotten it to work beyond giving me the string "<HTML>" so if anyone has some code that works to get a specific tag or an associativearray of tags it would be awesome.


    The trick is to use lazy operators (instead of greedy ones, which are the default). You get lazy operation by appending "?" to a match multiplier.

    e.g.
    With the string "<b>this is a bold string.</b> <i>this is an italicized string.</i> <b>this is another bold string</b>"
    With this content, the regex "<b>.*</b>" will match from the first <b> to the last </b>, which generally isn't what you want.
    If you change the regex to "<b>.*?</b>" you get a "lazy" match between the opening and closing bold tags, which tries to match the minimum possible, instead of the maximum possible, with the maximum possible match being the default behavior of + and * (and ? when used by itself).

    That said, there are other problems commonly encountered. using a regular expression to parse HTML is problematic in most cases. You really need to be able to make some assumptions about the specific content you are parsing.