Razorberry’s Adobe Flash Blog

November 2nd, 2007

Converting html entities in AS3

Posted by Ash in Actionscript
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
Macromedia XML News Aggregator