Converting html entities in AS3

Actionscript

I'm posting this here mainly because I always have to look it up. But maybe this post will help someone down the road.

Converting a string with html entities (such as &) to their respective symbols:

AS:


public function htmlUnescape(str:String):String
{
    return new XMLDocument(str).firstChild.nodeValue;
}

Converting a string so that it has html entities:

AS:


public function htmlEscape(str:String):String
{
    return XML( new XMLNode( XMLNodeType.TEXT_NODE, str ) ).toXMLString();
}
AS:


trace(htmlEscape("ham & eggs"))// ham & eggs
trace(htmlUnescape("ham & eggs"))// ham & eggs

UPDATE: AS2 versions by request.

AS:


function htmlUnescape(str:String):String
{
    return new XML(str).firstChild.nodeValue;
}

function htmlEscape(str:String):String
{
    return new XMLNode( 3, str ).toString();
}

This entry was posted on Friday, November 2nd, 2007 at 5:34 pm and is filed under Actionscript. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

24 Responses to “Converting html entities in AS3”

  1. Patrick Burt Says:

    I don't actually need to know this right now, but I will in about a week or so. Thanks m8. :)

  2. bobnik Says:

    Exactly what the doctor ordered. Thank you.

  3. bob Says:

    thanks! :mrgreen:

  4. Diabolo Says:

    Thank you!

  5. Monokai Says:

    This doesn't when your string contains "<" characters, any ideas?

  6. Ash Says:

    Monokai: Technically the htmlUnescape function shouldnt work for strings with < and > in, since those characters arent valid inside an escaped string anyway.
    However, you can replace those manually:
    str = str.replace(/</g,"&lt;").replace(/>/g,"&gt;")

  7. Monokai Says:

    Yes, that's true, they don't belong there. Thanks for your insight!

  8. Bevan Says:

    Thanks for posting.
    I did come across one little problem when using Air, if you pass in a null string it crashes Air (the ADL when debugging).
    So I made a small change:

    public function htmlEscape(str:String):String
    {
    if(!str) return "";
    return XML( new XMLNode( XMLNodeType.TEXT_NODE, str ) ).toXMLString();
    }

  9. nthitz Says:

    Hmm this does exactly what I was looking for, but surely there has to be a better way rather than XML hax?

  10. sam Says:

    This doesnt work if ur parsing HTML through it.

  11. kwasi Says:

    I have the following

    var unescapedHTML:String = new XMLDocument( html ).firstChild.nodeValue;
    /***/
    _text.htmlText = unescapedHTML;

    but for some reason the content of _text shows converted html entities therefor i'm seeing "blah blah blah"

    Using AS3, and _text is dynamically created as follows

    _text.type = TextFieldType.DYNAMIC;
    _text.selectable = false;
    _text.multiline = true;
    _text.wordWrap = true;
    _text.condenseWhite = true;

  12. kwasi Says:

    I seen the text with html tags (p)(font) blah blah (/font)(/p) "replaced with (), so browser doesn't render as HTML"

  13. kwasi Says:

    goto (for full post).
    http://www.actionscript.org/forums/showthread.php3?p=867110#post867110

  14. Nicolas Says:

    tf.htmlText = new XML();

    P.S.
    advice - don't use uppercase in tag names

  15. kwasi Says:

    Okay the solution you proposed now produces extra characters.
    &lt;p&gt;&lt;strong&gt;full content comes here,&nbsp;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&nbsp;&lt;/p&gt;&lt;p&gt;this is a new line in the document&lt;/p&gt;

    this is what is saved in the database, and when rendered I see content wiht out the HTML tags but the are funny blocks with in the text e.g. you hello my name is [] blah blah [].

    using code

    /***/
    public function set html( html:String ):void {
    /***/
    _text.htmlText = new XML( new XMLDocument( html ).firstChild.nodeValue );
    }

  16. Davide Says:

    Really thanks, you resolved me a tricky problem with combobox!
    Thaks again!
    Davide

  17. plauska Says:

    Hi man. I'm using AS2 and have the same problem. Would you help me out? I need a AS2 solution, pls...

  18. Ash Says:

    plauska: I've included the as2 versions in the post above. The functions are practically the same.

  19. Sean Says:

    I was preparing to write a function to handle this, given that I need to handle all of the really odd HTML entities as well as the common ones, but then I stumbled upon this. Thanks a lot for a clever solution to this problem!

  20. Johan Nyberg Says:

    Thanks! Really needed this.

  21. Casey Says:

    This won't work for ™

  22. Jloa Says:

    U would rather write a static utils class for it.

  23. ZREN Says:

    Thanks, it’s very helpful for me.
    http://www.as3tutorial.com is very helpful for beginners.

  24. codam Says:

    Here is a slight modification concerning AS2 code, otherwise gives a compilation error:

    /**
    * Encodes a html string using html entities.
    * @param str: the html sting to be encoded
    * @return a string html entities encoded
    */
    function htmlEntitiesEncode(str:String):String
    {
    return (new XMLNode( 3, str )).toString();
    }
    /**
    * Decodes a html string using html entities.
    * @param str: the html sting to be decoded
    * @return a string html entities decoded
    */
    function htmlEntitiesDecode(str:String):String
    {
    return (new XML(str)).firstChild.nodeValue;
    }

Leave a Reply