How to get Twitter Status with links

Found this gorgeous function to get the last Twitter Update from a user.

Features

  • It prints the links inside the tweets.
  • Also puts a link on a @user reply tweets

Code

function getTwitterStatus($userid,$x){
$url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=$x";

$xml = simplexml_load_file($url) or die('could not connect');
echo '

    ';
    foreach($xml->status as $status){
    $text = twitterify( $status->text );
    echo '

  • '.utf8_decode($text).'
  • ';
    }
    echo '

';
}

function twitterify($ret) {
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1\\2", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1
\\2", $ret);
$ret = preg_replace("/@(\w+)/", "
@\\1", $ret);
$ret = preg_replace("/#(\w+)/", "#\\1", $ret);
return $ret;
}

// Now let's print the Tweets.
// Put your own username.
// Set the number of tweets.
print_r(getTwitterStatus('quicoto_dev', 1));

About Rick

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.
3 comments

Leave a Reply

Add <code> Some Code </code> by using this tags.

*
*