开发者

What would be the regular expression for finding "RT @"?? And removing it as a potential result?

I am getting these results from my api query on twitter.

I would like to not display the Retweeted开发者_如何学JAVA ones.

How would i accomplish this in PHP?

RT @BarclaysWealth: RT @BarclaysStock: Investment ViewPoint - We take a look at what a hung parliament could mean for the UK economy http://bit.ly/OaYh7
From: InfoFocus: at: Fri, 07 May 2010 21:02:10 +0000

RT @BarclaysStock: Investment ViewPoint - We take a look at what a hung parliament could mean for the UK economy http://bit.ly/OaYh7
From: BARXdirect: at: Fri, 07 May 2010 16:35:56 +0000

Investment ViewPoint - We take a look at what a hung parliament could mean for the UK economy http://bit.ly/OaYh7
From: BarclaysStock: at: Fri, 07 May 2010 16:35:12 +0000

Cheers :),


If you don't need to use a regular expression, and assuming your tweets are in an array called $tweets:

foreach ($tweets as $tweet)
{
  if (strpos($tweet, "RT @") === FALSE)
  {
    print $tweet;
  }
}

See strpos() for details. Might be a tad faster than using regexs too.


$text = 'RT @BarclaysWealth: RT @BarclaysStock: Investment ViewPoint...';
if (preg_match('/^RT @/', $text)) {
 // this one starts with  RT @
} else {
 // does not start with RT @, so do something with it
}


<?php                                                                                                                                                             
$d = 'RT @BarclaysWealth: RT @BarclaysStock: Investment ViewPoint - We take a look at what a hung parliament could mean for the UK economy http://bit.ly/
From: InfoFocus: at: Fri, 07 May 2010 21:02:10 +0000                                                                                                     

RT @BarclaysStock: Investment ViewPoint - We take a look at what a hung parliament could mean for the UK economy http://bit.ly/OaYh7                     
From: BARXdirect: at: Fri, 07 May 2010 16:35:56 +0000                                                                                                    

Investment ViewPoint - We take a look at what a hung parliament could mean for the UK economy http://bit.ly/OaYh7                                        
From: BarclaysStock: at: Fri, 07 May 2010 16:35:12 +0000                                                                                                 

';                                                                                                                                                       

echo preg_replace('/(^|(?<=\n))RT @.*?\n\n/s', '', $d);                                                                                                  

gives

Investment ViewPoint - We take a look at what a hung parliament could mean for the UK economy http://bit.ly/OaYh7
From: BarclaysStock: at: Fri, 07 May 2010 16:35:12 +0000

If you don't want to remove only entries starting with RT @ but also the ones that have RT @ somewhere inside them use just:

echo preg_replace('/RT @.*?\n\n/s', '', $d);

If you have your entries already separated don't use regexp. Just iterate over them and check if strpos($entry, 'RT @') !== FALSE and if it does, remove this entry.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜