Twitter Tweet字符串编码实体实用程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Twitter Tweet字符串编码实体实用程序相关的知识,希望对你有一定的参考价值。

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_%]*).[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_%]*).[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. }

以上是关于Twitter Tweet字符串编码实体实用程序的主要内容,如果未能解决你的问题,请参考以下文章

修改tweet调用原点作为应用程序

使用npm twit将图像上传到Twitter时“媒体类型无法识别”

twitter元数据中的extended_tweet在哪里

Python 可视化Twitter中指定话题中Tweet的词汇频率

Twitter RSS提要解析器,用于在网站上显示tweet(包括缓存)

python check_twitter_friends_last_tweet_date.py