/** @name TwitterWidget @author Gabriele Romanato This class accepts two parameters passed to its constructor: 1. $username = Your Twitter username 2. $limit = How many tweets you want to display */ class TwitterWidget { protected $_username; protected $_limit; public function __construct($username, $limit) { $this->_username = $username; $this->_limit = $limit; } /** @param String $text The text of each tweet @returns String $replaced A string with all URLs turned into elements */ protected function _replaceTextURLs($text) { $re = '/(http:\/\/[^\s&]+)/'; $replaced = preg_replace($re, '$1', $text); return $replaced; } /** @param String $time A Unix timestamp @returns String The elapsed time for each tweet */ protected function _timeAgo($time) { $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); $lengths = array("60","60","24","7","4.35","12","10"); $now = time(); $difference = $now - $time; $tense = "ago"; for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { $difference /= $lengths[$j]; } $difference = round($difference); if($difference != 1) { $periods[$j].= "s"; } return "$difference $periods[$j] ago "; } /** @param None @returns The following markup string:

Tweet's content

Elapsed time for each tweet
*/ public function outputWidget() { $tweets = simplexml_load_file('http://twitter.com/statuses/user_timeline/'. $this->_username . '.rss'); $output = '
' . "\n"; $i = 0; do { $i++; $tweet = $tweets->channel->item[$i]; $text = str_replace($this->_username . ':', '', $tweet->title); $text = $this->_replaceTextURLs($text); $time = strtotime($tweet->pubDate); $output .= '
' . "\n". '

' . $text . '

' . "\n" . '
' . $this->_timeAgo($time) . '
' . "\n" . '
' . "\n"; } while($i < $this->_limit); $output .= '
' . "\n"; return $output; } }