How to get Twitter Status with links

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


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


function getTwitterStatus($userid,$x){
	$url = "$userid.xml?count=$x";
	$xml = simplexml_load_file($url) or die('could not connect');
		echo '<ul>';
		   foreach($xml->status as $status){
		   $text = twitterify( $status->text );
		   echo '<li>'.utf8_decode($text).'</li>';
		echo '</ul>';
	 function twitterify($ret) {
	  $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" >\\2", $ret);
	  $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" >\\2", $ret);
	  $ret = preg_replace("/@(\w+)/", "<a href=\"\\1\" >@\\1</a>", $ret);
	  $ret = preg_replace("/#(\w+)/", "<a href=\"\\1\" >#\\1</a>", $ret);
	return $ret;

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


  1. quicoto says:

    I had some codification problems. I added the iconv function in the return:

    return  iconv("ISO-8859-1", "UTF-8", $ret);
    Code language: PHP (php)
  2. JMLeon says:

    Nice trick, adding it to delicious.

    1. quicoto says:

      Glad you like it 😉

Leave a Reply

Your email address will not be published. Required fields are marked *