If you are using twitter’s API to display your tweets in your Website you have probably faced the problem that your mentions (e.g. @burnmind) and URLs are displayed as plain text and not as active links. You can fix this in a very easy way, using regular expressions in php.
Assuming that you have stored your tweet in the variable named $tweet, you’ll only need the following two lines of code to convert the mentions and URLs to links:
$tweet = preg_replace("/((http)+(s)?:\/\/[^<>\s]+)/i", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $tweet );
$tweet = preg_replace("/[@]+([A-Za-z0-9-_]+)/", "<a href=\"http://twitter.com/\\1\" target=\"_blank\">\\0</a>", $tweet );
If you don’t know how to retrieve your tweet using the twitter’s API you can learn how by reading the following tutorials:
how to retrieve and display your latest tweet using PHP
how to store and retrieve your latest tweet from an XML file
no comments | belongs to: tutorials | posted on September, 2009
I’m not a social media fan, but I thought to give twitter a try. I’m using it for a month now and I have a question for all the twitter users: How can you manage this enormous amount of information??
The first couple of days (when I was following about 80 users), I tried to read every single tweet while I was working on my computer. So, I scheduled the tweetdeck’s updates at a 5 minute interval. Big mistake! If I was lucky enough to read all the tweets before the next update, I had only 1-2 minutes to continue my work before the next one! Afterwards, I decided to read some tweets every couple of hours. It was ok, until I started to follow 250 users. There is no way to keep track of all those tweets! Of course, you need to follow to be followed, but I don’t think that anyone that follows 1000 users reads the tweets of even 100 of them (unless this is what he’s doing for a living). So, what’s the point?
There is very interesting information in there, but I have to spend at least 2-3 hours per day to read everything that interests me. In my twitter month, I stopped reading forums or exploring blogs that I like because I didn’t have the time anymore; twitter sucked it all!
My solution? I should either reduce the number of the users that I follow, or keep all of them (and even add more) creating a separate group of 30-50 users whose tweets I will actually read and interact with. But, isn’t it hypocritical?
I’d like to hear your thoughts on the subject!
5 comments | belongs to: urban legends | posted on September, 2009
Retrieving and displaying a tweet using the twitter API in real-time as discussed in “how to retrieve and display your latest tweet using PHP” can sometimes produce errors due to twitter’s inability to handle a lot of simultaneous request from multiple users. To deal with that, you can store the tweet in an XML file and retrieve it from there, so you don’t have to rely on twitter’s availability. This way is more efficient and will improve the loading time of the page.
First, you need to create a file named loadtweet.php which will load the latest tweet the usual way, will perform a check to find out if the result is an actual tweet and not an error message by computing the tweet’s length (it must have a maximum length of 140 characters) and will finally save the whole respond to an XML file called tweet.xml:
$twitter_url = 'http://twitter.com/statuses/user_timeline/yourTwitterUserName.xml?count=1';
$buffer = file_get_contents($twitter_url);
$xml = new SimpleXMLElement($buffer);
$status_item = $xml -> status;
$status = $status_item -> text;
$length = strlen($status);
if ($length < 141)
{
$file = fopen("tweet.xml", "w");
fwrite($file, $buffer);
fclose($file);
}
Your Web Server can be scheduled to execute this file automatically, for example every one minute, using a Cron Job (reffer to your Web Server’s Control Panel or your Hosting Company to find out how to schedule a Cron Job).
To retrieve the tweet from the XML file and display it wherever you want in your page, use the following code:
$xml = simplexml_load_file("tweet.xml");
$status_item = $xml -> status;
$status = $status_item -> text;
echo $status;
2 comments | belongs to: tutorials | posted on September, 2009