Jump to content

Exclude @replies from twitter feed


Merdok

Recommended Posts

Hi Guys,

 

I am using a Twitter feed parser on my website which takes ALL tweets from myself and my mate and displays the last 10 on our website, however I want the results to ignore any which are replies (have an @ as the first character for those who don't use Twitter).

 

However I did not write this script and the support for the person who did is limited (or possibly just non-existant, I have never had a reply from him). The code parses the twitter search api results in an XML format which comes out like this:

 

 <entry>
    <id>tag:search.twitter.com,2005:11142957484</id>
    <published>2010-03-27T13:14:41Z</published>
    <link type="text/html" href="http://twitter.com/alexbward/statuses/11142957484" rel="alternate"/>
    <title>@cbo79 Don't worry, the effects will be very localised, most likely limited to merely our own galaxy.</title>
    <content type="html"><a href="http://twitter.com/cbo79">@cbo79</a> Don&apos;t worry, the effects will be very localised, most likely limited to merely our own galaxy.</content>
    <updated>2010-03-27T13:14:41Z</updated>
    <link type="image/png" href="http://a3.twimg.com/profile_images/688005567/twitterProfilePhoto_normal.jpg" rel="image"/>
    <twitter:geo>
    </twitter:geo>
    <twitter:source><a href="http://www.tweetdeck.com" rel="nofollow">TweetDeck</a></twitter:source>
    <twitter:lang>en</twitter:lang>
    <author>
      <name>alexbward (Alex Ward)</name>
      <uri>http://twitter.com/alexbward</uri>
    </author>
  </entry>

 

As this starts with an '@' this doesn't want to be displayed on the page, however I've no clue how to do this. The full code for the feed parser is below.

 

<?php
$twitterAuthors = mysql_query("SELECT usr_twitter FROM core_users WHERE usr_access_lvl <=4");
$t = mysql_num_rows($twitterAuthors);
while(list($usr_twitter) = mysql_fetch_array($twitterAuthors, MYSQL_NUM)) {
$tweetCount++;
$twits .= $usr_twitter;
if ($tweetCount != $t) {
$twits .= ' ';
}
}
/*
Parse Twitter Feeds
*/
$usernames = $twits;
$limit = 6;
$username_for_feed = str_replace(" ", "+OR+from%3A", $usernames);
$feed = "http://search.twitter.com/search.atom?q=from%3A" . $username_for_feed . "&rpp=" . $limit;
$usernames_for_file = str_replace(" ", "-", $usernames);
$cache_file = dirname(__FILE__).'/cache/' . $usernames_for_file . '-twitter-cache';
$last = filemtime($cache_file);
$now = time();
$interval = 600; // ten minutes
// check the cache file
if ( !$last || (( $now - $last ) > $interval) ) {
	// cache file doesn't exist, or is old, so refresh it
	$cache_rss = file_get_contents($feed);
	if (!$cache_rss) {
		// we didn't get anything back from twitter
		echo "<!-- ERROR: Twitter feed was blank! Using cache file. -->";
	} else {
		// we got good results from twitter
		echo "<!-- SUCCESS: Twitter feed used to update cache file -->";
		$cache_static = fopen($cache_file, 'wb');
		fwrite($cache_static, serialize($cache_rss));
		fclose($cache_static);
	}
	// read from the cache file
	$rss = @unserialize(file_get_contents($cache_file));
}
else {
	// cache file is fresh enough, so read from it
	echo "<!-- SUCCESS: Cache file was recent enough to read from -->";
	$rss = @unserialize(file_get_contents($cache_file));
}
// clean up and output the twitter feed
$feed = str_replace("&", "&", $rss);
$feed = str_replace("<", "<", $feed);
$feed = str_replace(">", ">", $feed);
$clean = explode("<entry>", $feed);
$clean = str_replace(""", "'", $clean);
$clean = str_replace("'", "'", $clean);
$amount = count($clean) - 1;
if ($amount) { // are there any tweets?
	for ($i = 1; $i <= $amount; $i++) {
		$entry_close = explode("</entry>", $clean[$i]);
		$clean_content_1 = explode("<content type=\"html\">", $entry_close[0]);
		$clean_content = explode("</content>", $clean_content_1[1]);
		$clean_name_2 = explode("<name>", $entry_close[0]);
		$clean_name_1 = explode("(", $clean_name_2[1]);
		$clean_name = explode(")</name>", $clean_name_1[1]);
		$clean_user = explode(" (", $clean_name_2[1]);
		$clean_lower_user = strtolower($clean_user[0]);
		$clean_uri_1 = explode("<uri>", $entry_close[0]);
		$clean_uri = explode("</uri>", $clean_uri_1[1]);
		$clean_time_1 = explode("<published>", $entry_close[0]);
		$clean_time = explode("</published>", $clean_time_1[1]);
		$twitterPics = mysql_query("SELECT usr_avatar FROM core_users WHERE usr_twitter = '".$clean_lower_user."'");
		list($usr_avatar) = mysql_fetch_array($twitterPics, MYSQL_NUM);
			?>
<div class="tweet noPrint"><a href="http://www.twitter.com/<?php echo $clean_lower_user; ?>"><img src="<?php echo $siteroot ?>/Scripts/phpThumb/phpThumb.php?src=<?php echo $usr_avatar ?>&w=40&h=40&zc=c" title="View <?php echo $clean_lower_user; ?>'s profile" alt="<?php echo $clean_lower_user; ?>'s profile picture"/></a><span><?php echo $clean_content[0]; ?></span></div>
<?php }
} else { // if there aren't any tweets
	?>
<div class="tweet noPrint"><span> It's hard to believe but we have not posted any tweets lately. I hope we are not dead!</span> </div>
<?php
}
?>

 

I have modified it a bit so it will return information from my user database instead of being a manual entry but thats about as far as I can work it out.

Link to comment
https://forums.phpfreaks.com/topic/199820-exclude-replies-from-twitter-feed/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.