Posted By

ninjaparade on 03/16/11


Tagged

regex utility twitter as3


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

sheshu000


Twitter Tweet String Encode Entity Utility


 / Published in: ActionScript 3
 

Encode a Tweet from a Twitter search query wrapping all hash tags, user names and urls with herf entities with one function call

  1. package {
  2. /**
  3. * @author Ninjaparade
  4. */
  5. public class TwitterStringUtil
  6. {
  7. public static function encodeTwitterString(tweet : String) : String
  8. {
  9. var twitterHash : RegExp = /(\#[a-zA-Z0-9_%]*)/g;
  10. var twitterUser : RegExp = /(\@[a-zA-Z0-9_%]+)/ig;
  11. var twitterLink : RegExp = /(https?:\/\/)?(www\.)?([a-zA-Z0-9_%]*)\b\.[a-z]{2,4}(\.[a-z]{2})?((\/[a-zA-Z0-9_%]*)+)?(\.[a-z]*)?/g;
  12. var str : String = tweet;
  13. var arr : Object = str.match(twitterLink);
  14.  
  15. if (arr != null)
  16. {
  17. for each ( var s:String in arr ) {
  18. str = str.replace(s, encodeURL(s));
  19. }
  20. }
  21. arr = str.match(twitterHash);
  22. if (arr != null)
  23. {
  24. for each (var k : String in arr) {
  25. str = str.replace(k, replaceHashtoURL(k));
  26. }
  27. }
  28. arr = str.match(twitterUser);
  29. if (arr != null)
  30. {
  31. for each (var j : String in arr) {
  32. str = str.replace(j, replaceURL(j));
  33. // str = "<a href = \"http://www.twitter.com/" + j.substr(1, j.length) + "\" " + "target=\"_blank\">" + j + "</a>";
  34. }
  35. }
  36. return str;
  37. }
  38.  
  39. public static function searchTwitterHash(value : String) : String
  40. {
  41. var pattern : RegExp = /(\#[a-zA-Z0-9_%]*)/g;
  42. var str : String = value;
  43. var arr : Object = value.match(pattern);
  44.  
  45. // if(arr != null) {
  46. for each ( var s:String in arr ) {
  47. str = str.replace(s, replaceHashtoURL(s));
  48. // }
  49. }
  50. return str;
  51. }
  52.  
  53. public static function searchTwitterUser(value : String) : String
  54. {
  55. var pattern : RegExp = /(\@[a-zA-Z0-9_%]+)/ig;
  56. var str : String = value;
  57. var arr : Object = value.match(pattern);
  58.  
  59. for each ( var s:String in arr ) {
  60. str = str.replace(s, replaceURL(s));
  61. }
  62. return str;
  63. }
  64.  
  65. public static function searchTwitterURL(value : String) : String
  66. {
  67. var pattern : RegExp = /(https?:\/\/)?(www\.)?([a-zA-Z0-9_%]*)\b\.[a-z]{2,4}(\.[a-z]{2})?((\/[a-zA-Z0-9_%]*)+)?(\.[a-z]*)?/g;
  68. var str : String = value;
  69. var arr : Object = value.match(pattern);
  70.  
  71. if (arr != null)
  72. {
  73. for each ( var s:String in arr ) {
  74. str = str.replace(s, encodeURL(s));
  75. }
  76. }
  77. return str;
  78. }
  79.  
  80. public static function replaceURL(value : String) : String
  81. {
  82. var str : String = "<a href = \"http://www.twitter.com/" + value.substr(1, value.length) + "\" " + "target=\"_blank\">" + value + "</a>";
  83. return str;
  84. }
  85.  
  86. public static function replaceHashtoURL(value : String) : String
  87. {
  88. var str : String = "<a href = \"http://twitter.com/#search?q=%23" + value.substr(1, value.length) + "\" " + "target=\"_blank\">" + value + "</a>";
  89. return str;
  90. }
  91.  
  92. public static function encodeURL(value : String) : String
  93. {
  94. var str : String = "<a href = " + "\"" + value + "\" " + "target=\"_blank\">" + value + "</a>";
  95. return str;
  96. }
  97. }
  98. }

Report this snippet  

You need to login to post a comment.