Flex/Actionscript 3.0 Strip HTML Tags Function
I needed a function to strip out html tags from a text input, but still let me specify allowable tags.
Instead of spending time figuring out the regular expressions needed to pull it off and becoming a better programmer, I figured why repeat work someone else has probably already done.. I mean I could be a busy man. Anyway I found this great function on Flexer.info [link]. But after trying it out I noticed that the one tag I really really wanted to be parsed out iframe wasn't. It seems because I had specified i as an allowable tag it was also accepting iframe.
So with all due respect to Andrei, below is the revised function with the security hole patched.
All I changed was near the bottom where it determines if it's an allowable tag or not the reg exp was
<\/?" + tagsToKeep[j] + "[^<>]*?>
which allowed any character to follow the allowed tag as long as it wasn't a nested tag, which included frame following i. This will also support self closing tags.
// strips htmltags // @param html - string to parse // @param tags - tags to ignore public static function stripHtmlTags(html:String, tags:String = ""):String { var tagsToBeKept:Array = new Array(); if (tags.length > 0) tagsToBeKept = tags.split(new RegExp("\\s*,\\s*")); var tagsToKeep:Array = new Array(); for (var i:int = 0; i < tagsToBeKept.length; i++) { if (tagsToBeKept[i] != null && tagsToBeKept[i] != "") tagsToKeep.push(tagsToBeKept[i]); } var toBeRemoved:Array = new Array(); var tagRegExp:RegExp = new RegExp("<([^>\\s]+)(\\s[^>]+)*>", "g"); var foundedStrings:Array = html.match(tagRegExp); for (i = 0; i < foundedStrings.length; i++) { var tagFlag:Boolean = false; if (tagsToKeep != null) { for (var j:int = 0; j < tagsToKeep.length; j++) { var tmpRegExp:RegExp = new RegExp("<\/?" + tagsToKeep[j] + " ?/?>", "i"); var tmpStr:String = foundedStrings[i] as String; if (tmpStr.search(tmpRegExp) != -1) tagFlag = true; } } if (!tagFlag) toBeRemoved.push(foundedStrings[i]); } for (i = 0; i < toBeRemoved.length; i++) { var tmpRE:RegExp = new RegExp("([\+\*\$\/])","g"); var tmpRemRE:RegExp = new RegExp((toBeRemoved[i] as String).replace(tmpRE, "\\$1"),"g"); html = html.replace(tmpRemRE, ""); } return html; }
1 Comment | Jan 22, 2010