Sunday, March 16, 2008

Using TextField for HTML Stripping

This may be something commonly used already, but it's the first time I've run into the need. I am working on an application where I needed to strip all html tags out of a string. First over-complicating things, I looked around the net and found several functions that will parse out the html tags. Then it hit me, there's an easier way - and it's build right into Flash. Using the TextField class, you can easily rip the html tags out - and still keep your original string intact. Since TextField has both an htmlText property and a text property, you can write to one and read from the other:

var htmlStripper:TextField = new TextField();
htmlStripper.htmlText = originalHtmlString;

var strippedString:String = htmlStripper.text;