-1

I have a twitter feed at the bottom of my website I am in the process of building: http://matthewkcooper.netii.net/

As you can see it is outputting url links at hashtags that are not clickable. How can I make these links active?

Adrian Heine
  • 4,051
  • 2
  • 30
  • 43
  • Please show the code you're using to output your twitter feed. Are you using a library, or have you written this yourself? – Phill Sparks Apr 13 '13 at 20:47
  • I am currently using this PHP script which includes caching of tweets: http://www.codepotato.co.uk/2011/08/11/simple-php-twitter-feed-with-caching-2/ – Matthew Cooper Apr 13 '13 at 20:59
  • There's a function in http://stackoverflow.com/q/3595521/1269513 that'll hook up autolinking for you. You can call it on the 'desc' either when you output at the bottom of the script, or when you push them on to the array (around line 60) – Phill Sparks Apr 13 '13 at 21:19
  • Thanks Phill. I'm having difficulty getting it to work though. Could you provide the exact syntax? I've tried "echo autolink('');" and placed the function at the top of the script – Matthew Cooper Apr 13 '13 at 21:38
  • If you copied the function exactly from the question I linked you to then it's `twitterify()` not `autolink()`, unless you renamed the function? What error message are you getting? Or is it just not adding links? – Phill Sparks Apr 13 '13 at 21:43
  • Got it working as you can see.. http://matthewkcooper.netii.net/ Thanks Phill! – Matthew Cooper Apr 13 '13 at 21:56
  • You posted your success just as I was writing up an answer - good timing? – Phill Sparks Apr 13 '13 at 21:58
  • Haha definitely good timing. It may be worth mentioning I had to change "http://search.twitter.com/search?q=\\1\" to "https://twitter.com/search?q=%23/\\1\" to get properly working hashtag links – Matthew Cooper Apr 13 '13 at 22:05
  • Good catch! I can see why that would be necessary. If you're happy with my answer please mark it accepted, or let me know if you need any more changes :) – Phill Sparks Apr 13 '13 at 22:10
  • Nope, very happy, it has been accepted. Thanks again! :) – Matthew Cooper Apr 13 '13 at 22:18

1 Answers1

0

I found an autolink function from Why does this function not autolink? and added it near the bottom of the script you linked to, calling it around $row['desc'] in the echo.

function twitterify($ret)
{
    $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
    $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
    $ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret);
    $ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=%23\\1\" target=\"_blank\">#\\1</a>", $ret);
    return $ret;
}

foreach($feed as $row)
{
    echo('<div>'.twitterify($row['desc']).'<span><a href="'.$row['link'].'">'.relativedate(time()-strtotime($row['date'])).' ago</a></span></div>');
}
Community
  • 1
  • 1
Phill Sparks
  • 20,000
  • 3
  • 33
  • 46