Converting html entities in AS3
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;
}
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();
}
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
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 htmlUnescape(str:String):String
{
return new XML(str).firstChild.nodeValue;
}
function htmlEscape(str:String):String
{
return new XMLNode( 3, str ).toString();
}


November 8th, 2007 at 1:35 pm
I don't actually need to know this right now, but I will in about a week or so. Thanks m8.
January 29th, 2008 at 11:49 pm
Exactly what the doctor ordered. Thank you.
March 21st, 2008 at 5:20 pm
thanks!
April 2nd, 2008 at 10:50 am
Thank you!
April 23rd, 2008 at 8:06 am
This doesn't when your string contains "<" characters, any ideas?
April 23rd, 2008 at 4:36 pm
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,"<").replace(/>/g,">")
April 25th, 2008 at 9:46 am
Yes, that's true, they don't belong there. Thanks for your insight!
August 28th, 2008 at 5:05 am
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();
}
December 29th, 2008 at 5:21 pm
Hmm this does exactly what I was looking for, but surely there has to be a better way rather than XML hax?
January 19th, 2009 at 5:52 am
This doesnt work if ur parsing HTML through it.
April 3rd, 2009 at 2:04 am
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;
April 3rd, 2009 at 2:09 am
I seen the text with html tags (p)(font) blah blah (/font)(/p) "replaced with (), so browser doesn't render as HTML"
April 3rd, 2009 at 11:10 pm
goto (for full post).
http://www.actionscript.org/forums/showthread.php3?p=867110#post867110
April 7th, 2009 at 9:31 am
tf.htmlText = new XML();
P.S.
advice - don't use uppercase in tag names
May 20th, 2009 at 8:46 am
Okay the solution you proposed now produces extra characters.
<p><strong>full content comes here, </strong></p><p> </p><p>this is a new line in the document</p>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 );
}
July 21st, 2009 at 6:29 am
Really thanks, you resolved me a tricky problem with combobox!
Thaks again!
Davide
August 18th, 2009 at 9:28 am
Hi man. I'm using AS2 and have the same problem. Would you help me out? I need a AS2 solution, pls...
August 19th, 2009 at 9:56 am
plauska: I've included the as2 versions in the post above. The functions are practically the same.
August 25th, 2009 at 1:06 am
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!
October 1st, 2009 at 8:41 am
Thanks! Really needed this.
November 10th, 2009 at 8:04 pm
This won't work for ™
December 21st, 2009 at 5:39 am
U would rather write a static utils class for it.
January 13th, 2011 at 5:30 pm
Thanks, it’s very helpful for me.
http://www.as3tutorial.com is very helpful for beginners.
February 9th, 2011 at 9:21 am
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;
}