Parse a twitter feed with php (part 2)


I posted on this previously, but looking back think the code may have been a bit OTT for some people. Here is the VERY cut down version of that php code that will parse a twitter feed as plain text:


$twitterRSS = simplexml_load_file( ‘; ); //Noel’s twitter

$i = 0;

while ($i <= 4) {

$description =            $twitterRSS->channel->item[$i]->description;

$pubDate =                               $twitterRSS->channel->item[$i]->pubDate;

$guid =                                                 $twitterRSS->channel->item[$i]->guid;

echo $description;

echo ‘<br />’;

echo $pubDate;

echo ‘<br /><br />’;




Not much to it really, but a nightmare if you’re not used to parsing xml with php.

Below… is the very OTT version:o) this will additionally strip out the user name at the beginning of each tweet and format the date. It will also make the date a hyperlink back to the original twitter post and turn the first instance of a hyperlink into one that works (as opposed to plain text). There was also a little bug previously (sorry) which I’ve ironed out, oh and comments galore!

Please feel free to use it, and maybe give me a mention:o)


$twitterRSS = simplexml_load_file( ‘; ); //Noel’s twitter

$i = 0;
while ($i <= 4) {

$description =    $twitterRSS->channel->item[$i]->description;
$pubDate =              $twitterRSS->channel->item[$i]->pubDate;
$guid =                  $twitterRSS->channel->item[$i]->guid;

//remove username, colon, and single white space…
$colon = ‘:’;
$positionColon = strpos($description, $colon);
$description = substr_replace ($description, ” , 0 , ($positionColon +2) );

//find the position of ‘http://&#8217;
$http = ‘http://&#8217;;
$positionHttp = strpos  ( $description  , $http );

if ($positionHttp == !NULL) //i.e. there is a http:// link, then execute the following code

//OK, is there any white space after the http:// ?
//if there is then the link is probably in the middle somewhere…
//if there is no white space the link is probably at the end.

//find the first white space after the http://
//we will use strpos again, but this time include the offset parameter to choose where to start our search…
$whiteSpaceAfterHttp = strpos  ( $description , ‘ ‘ , $positionHttp );

if ($whiteSpaceAfterHttp > 0) { //i.e. there is one…
$linkLastChar = $whiteSpaceAfterHttp;

if ($whiteSpaceAfterHttp == NULL) { // i.e. no white space/link is at the end
$linkLastChar = strlen ( $description );

//we now have the beginning and end positions of the http link
// $positionHttp and $linkLastChar
//let’s get the link length…
$linkLength = ($linkLastChar – $positionHttp);

//let’s grab the link that needs ‘fixing’…
$oldLink = substr ( $description , $positionHttp , $linkLength );

//let’s add html tags to the old link and call it $newLink
$newLink = ‘<a href=”‘ . $oldLink . ‘”>’ . $oldLink . ‘</a>’;

//let’s swap the old link for the new link in the string…
$description = substr_replace  ( $description  , $newLink  , $positionHttp , $linkLength );


//ok, so the RSS date in a twitter tweet comes out something link this:
//Mon, 04 Oct 2010 11:37:43 +0000
//remove the first 5 characters…
$pubDate = substr_replace  ( $pubDate , ‘ ‘ , 0 , 5 );

//remove the remaining 21 characters from position 7 onwards…
$pubDate = substr_replace  ( $pubDate , ‘ ‘ , 7 , 21 );

//add the twitter link to the date with html…
$pubDate = ‘<a href=”‘ . $guid . ‘”>’ . $pubDate . ‘</a>’;

echo ‘<p>’;
echo $description;
echo ‘</p>’;

echo ‘<p>’;
echo $pubDate;
echo ‘</p>’;





No Responses Yet to “Parse a twitter feed with php (part 2)”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: