Skip to content

Instantly share code, notes, and snippets.

@coreyweb
Created January 21, 2013 22:04

Revisions

  1. coreyweb created this gist Jan 21, 2013.
    22 changes: 22 additions & 0 deletions tweet linkifier
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <?php
    // Convert URL's with protocol prefix
    $tweet = ereg_replace("[a-zA-Z]+://([-]*[.]?[a-zA-Z0-9_/-?&%])*", "<a href=\"\\0\">\\0</a>", $tweet);

    //Convert URL with just www.
    $tweet = ereg_replace("(^| |\n)(www([-]*[.]?[a-zA-Z0-9_/-?&%])*)", "\\1<a href=\"http://\\2\">\\2</a>", $tweet);

    //Convert # hashtags
    $tweet = ereg_replace("(^| |\n)(\#([-]*[.]?[a-zA-Z0-9_/-?&%])*)", "\\1<a href=\"http://search.twitter.com/search?q=\\2\">\\2</a>", $tweet);
    $tweet = str_replace("/#", "/", $tweet);

    //Convert @ replies
    $tweet = ereg_replace("(^| |\n)(\@([-]*[.]?[a-zA-Z0-9_/-?&%])*)", "\\1@<a href=\"http://www.twitter.com/\\2\">\\2</a>", $tweet);
    $tweet = str_replace("/@", "/", $tweet);
    $tweet = str_replace(">@", ">", $tweet);

    // Make the time prettier
    $createstamp = $post->get('pubDate');
    $relative_time = niceTime(strtotime($createstamp));
    ?>

    <?php echo $tweet ?> <span>posted <a href="<?php echo $tweet_link ?>"><?php echo $relative_time ?></a>&nbsp;</span>