HTML Character Codes in Flash

I don’t know how this escaped me for so long, but the other day I realized that Flash doesn’t understand HTML character entity reference codes. Codes other than & and a few others won’t render out to their appropriate symbol. So for example, if I need to display a special character in an HTML page, I would normally use the &+code+; in order to display it. So “copyright ©2009 eric decker” would be “copyright © 2009 eric decker”. So it would make sense that if I bring in this copy into flash, set it to a textfield with the set htmlText method, that it would have the same result. However, it doesn’t. &copy will read exactly as &copy.  Flash does, however, understand numeric reference codes, which look like ©. Personally, I find the name-based entity codes easier to use and seem like they are more widely used (although I could be wrong about that, I’m not a HTML guy).

Anyways, determined to be able to use © instead of the more ambiguous © I wrote a class that is able to understand the entity codes. The reasons for this were 1. as I just mentioned, the name based codes are easier to read and 2. sometime you’re not in charge of your source copy, and it might contain entity based symbols instead of numeric and of course 3. developers are a little neurotic like that and sometimes ‘do it for the sake of doing it.’ (That could be  a T-Shirt.) I was able to write a pretty simple converter using a regular expression and a lookup dictionary. You can download the class or view it as text.

The class contains one accessible static function that you pass your raw text to and it returns the new formatted text: myTextField.htmlText = HTMLTextUtils.formatHTMLTokens(myString);  Better yet, if you have a custom TextField class that you’ve extended, you can override the set htmlText function to have it always use this function.

Grant Skinner’s RegExr is a huge help when doing anything with RegEx.

4 thoughts on “HTML Character Codes in Flash”

Leave a Reply

Your email address will not be published. Required fields are marked *