Convert a link on a Twitter status to clickable url. Plus @ and #


/ Published in: PHP
Save to your folder(s)

To convert links on a twitter status to a clickable url. Also convert @ to follow link, and # to search


Copy this code and paste it in your HTML
  1. /**
  2. *
  3. * twitterStatusUrlConverter
  4. *
  5. * To convert links on a twitter status to a clickable url. Also convert @ to follow link, and # to search
  6. *
  7. * @author: Mardix - http://mardix.wordpress.com, http://www.givemebeats.net
  8. * @date: March 16 2009
  9. * @license: LGPL (I don't care, it's free lol)
  10. *
  11. * @param string : the status
  12. * @param bool : true|false, allow target _blank
  13. * @param int : to truncate a link to max length
  14. * @return String
  15. *
  16. * */
  17. function twitterStatusUrlConverter($status,$targetBlank=true,$linkMaxLen=250){
  18.  
  19. // The target
  20. $target=$targetBlank ? " target=\"_blank\" " : "";
  21.  
  22. // convert link to url
  23. $status = preg_replace("/((http:\/\/|https:\/\/)[^ )
  24. ]+)/e", "'<a href=\"$1\" title=\"$1\" $target >'. ((strlen('$1')>=$linkMaxLen ? substr('$1',0,$linkMaxLen).'...':'$1')).'</a>'", $status);
  25.  
  26. // convert @ to follow
  27. $status = preg_replace("/(@([_a-z0-9\-]+))/i","<a href=\"http://twitter.com/$2\" title=\"Follow $2\" $target >$1</a>",$status);
  28.  
  29. // convert # to search
  30. $status = preg_replace("/(#([_a-z0-9\-]+))/i","<a href=\"http://search.twitter.com/search?q=%23$2\" title=\"Search $1\" $target >$1</a>",$status);
  31.  
  32. // return the status
  33. return $status;
  34. }

URL: http://www.givemebeats.net

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.