除了php twitter状态脚本

时间:2009-07-07 23:34:19

标签: php twitter

我正在使用下面的脚本显示我的推特状态,但是想添加选项以不显示以@username开头的推文。我已经完成了它,因此它不会显示我的用户名,但是不希望显示回复给其他用户的推文。

由于

$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;
    }
}

(我试图将超链接伪装成无法发布多个超链接)

2 个答案:

答案 0 :(得分:2)

听起来你想添加这样的条件:

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

$tweet = substr($tweet, stripos($tweet, ':') + 1);之后。

答案 1 :(得分:0)

if(preg_match('/(|\s)@([0-9a-zA-Z]{3,})/', $tweet))
    continue;

更完整的比赛。在某些情况下,另一个将不正确匹配。