Posted By

mardix on 03/16/09


Tagged

php convert twitter lin kurl


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

fruehjahr
jackola
muoto
xida
berkleebassist
Priestd09


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


 / Published in: PHP
 

URL: http://www.givemebeats.net

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

  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. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: jackola on August 12, 2009

I made a slight change in mine in order to catch links on twitter that only have www and no http...

// convert link to url $status = preg_replace("/((http:\/\/|https:\/\/|www.)[^ )]+)/e", "''. ((strlen('$1')>=$linkMaxLen ? substr('$1',0,$linkMaxLen).'...':'$1')).''", $status);

// go back and make sure there's no

Posted By: jackola on August 12, 2009

whoops.

// convert link to url $status = preg_replace("/((http:\/\/|https:\/\/|www.)[^ )]+)/e", "''. ((strlen('$1')>=$linkMaxLen ? substr('$1',0,$linkMaxLen).'...':'$1')).''", $status);

// go back and make sure there's no a href="www. $status = str_replace("=\"www.","=\"http://www.", $status);

Posted By: jackola on August 12, 2009

Bah. Made it all in there but the last part didn't go onto a new line.

$status = str_replace("=\"www.","=\"http://www.", $status);

Posted By: xida on March 16, 2010

how would you handle links without www. ? Like twitter.com ?

Posted By: xida on March 16, 2010

how would you handle links without www. ? Like twitter.com ?

Posted By: xida on March 16, 2010

how would you handle links without www. ? Like twitter.com ?

Posted By: xida on March 16, 2010

how would you handle links without www. ? Like twitter.com ?

Posted By: xida on March 16, 2010

how would you handle links without www. ? Like twitter.com ?

You need to login to post a comment.