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)

About Ricard Torres

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

@ricard_dev @ricard_dev

📝 Blog 🎙 Podcast


Leave a Reply

Add <code> Some Code </code> if you need to.