views:

284

answers:

2

I'm using the script below to display my twitter status, but would like to add the option to not display tweets that start with a @username. I've done it so it won't display my username, but would like not to display tweets replied to another user.

Thanks

$doc = new DOMDocument();
# load the RSS
if($doc->load('twitterurlrss')) {
    # number of tweets to display.  20 is the maximum $max_tweets = 3;    
    $i = 1;
    foreach($doc->getElementsByTagName('item') as $node) {
        # fetch the title from the RSS feed
        $tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;
        $date = $node->getElementsByTagName('pubDate')->item(0)->nodeValue;
        $link = $node->getElementsByTagName('link')->item(0)->nodeValue;
        # the title of each tweet starts with "username: " which I want to remove
        $tweet = substr($tweet, stripos($tweet, ':') + 1);
        $date = date("dS F Y", strtotime($date));  
        # Turn URLs into links
        $tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',  '<a href="$1">$1</a>', $tweet);
        # Turn @replies into links
        $tweet = preg_replace("/@([0-9a-zA-Z]+)/", "<a href=\"twitterurl/$1\">@$1</a>", $tweet);
        # Turn & into &amp;
        $tweet = preg_replace('@&@',  '&amp;', $tweet);
        if($i%2 == 0) {
            echo "<div class=\"three-col center\"><p>" . $tweet . "<br /><span class=\"quiet\"><ahref=\"" . $link . "\">". $date . "</a></span></p></div>\n";
        } else {
            echo "<div class=\"three-col\"><p>" . $tweet . "<br /><span class=\"quiet\"><ahref=\"" . $link . "\">" . $date . "</a></span></p></div>\n";
        }
        if($i++ >= $max_tweets)
            break;
    }
}

(I've tried to disguise the hyperlinks as unable to post more then one)

+1  A: 

Sounds like you want to add a condition like this:

if(preg_match('/^\s*@([0-9a-zA-Z]+)/', $tweet))
    continue;

after $tweet = substr($tweet, stripos($tweet, ':') + 1);.

chaos
Thanks chaos (again), for the quick reply and solving my question.
You're welcome. :)
chaos
A: 
if(preg_match('/(|\s)@([0-9a-zA-Z]{3,})/', $tweet))
    continue;

A more complete match. The other will match incorrectly in some cases.

bucabay