Strip HTML tags from RSS


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public static function stripHtmlTags(html:String, tags:String = ""):String
  2. {
  3. var tagsToBeKept:Array = new Array();
  4. if (tags.length > 0)
  5. tagsToBeKept = tags.split(new RegExp("\\s*,\\s*"));
  6.  
  7. var tagsToKeep:Array = new Array();
  8. for (var i:int = 0; i < tagsToBeKept.length; i++)
  9. {
  10. if (tagsToBeKept[i] != null && tagsToBeKept[i] != "")
  11. tagsToKeep.push(tagsToBeKept[i]);
  12. }
  13.  
  14. var toBeRemoved:Array = new Array();
  15. var tagRegExp:RegExp = new RegExp("<([^>\\s]+)(\\s[^>]+)*>", "g");
  16.  
  17. var foundedStrings:Array = html.match(tagRegExp);
  18. for (i = 0; i < foundedStrings.length; i++)
  19. {
  20. var tagFlag:Boolean = false;
  21. if (tagsToKeep != null)
  22. {
  23. for (var j:int = 0; j < tagsToKeep.length; j++)
  24. {
  25. var tmpRegExp:RegExp = new RegExp("<\/?" + tagsToKeep[j] + "( [^<>]*)*>", "i");
  26. var tmpStr:String = foundedStrings[i] as String;
  27. if (tmpStr.search(tmpRegExp) != -1)
  28. tagFlag = true;
  29. }
  30. }
  31. if (!tagFlag)
  32. toBeRemoved.push(foundedStrings[i]);
  33. }
  34. for (i = 0; i < toBeRemoved.length; i++)
  35. {
  36. var tmpRE:RegExp = new RegExp("([\+\*\$\/])","g");
  37. var tmpRemRE:RegExp = new RegExp((toBeRemoved[i] as String).replace(tmpRE, "\\$1"),"g");
  38. html = html.replace(tmpRemRE, "");
  39. }
  40. return html;
  41. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.