twitter_stream_parse_tweets

The Twitter Stream twitter stream parse tweets function.

Description

twitter_stream_parse_tweets( $content, $r ); 

Parameters (2)

0. $content
The content.
1. $r
The r.

Usage

  1. if ( !function_exists( 'twitter_stream_parse_tweets' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'twitter-stream/twitter-stream.php'; 
  3.  
  4. // The content. 
  5. $content = null; 
  6.  
  7. // The r. 
  8. $r = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = twitter_stream_parse_tweets($content, $r); 
  12.  

Defined (1)

The function is defined in the following location(s).

/twitter-stream.php  
  1. function twitter_stream_parse_tweets($content, $r) { 
  2.  
  3. //Check if content is empty 
  4. if(!$content) 
  5. return false; 
  6.  
  7. //Check for aerrorreturned from Twitter 
  8. if(isset($content->errors)) 
  9. return array( error => $content->errors[0]->message ); 
  10.  
  11. $followers = $content[0]->user->followers_count; 
  12. $username = $content[0]->user->screen_name; 
  13. $o = ''; 
  14. foreach($content as $tweet) { 
  15.  
  16. //Find all URL's mentioned and store them in $matches. 
  17. //$pattern = "/(http:\/\/|https:\/\/)?(?(1)(\d{1, 3}\.\d{1, 3}\.\d{1, 3}\.\d{1, 3}|([-a-z0-9_]+\.)?[a-z][-a-z0-9]+\.[a-z]+(\.[a-z]{2, 2})?)|(www\.[a-z][-a-z0-9]+\.[a-z]+(\.[a-z]{2, 2})?))\/?[a-z0-9._\/~#&=;%+?-]+[a-z0-9\/#=?]{1, 1}/is"; 
  18. //New regex pattern to match t.co urls thanks to Jonny Vaughan 
  19. $pattern = "/(http:\/\/|https:\/\/)?(?(1)(\d{1, 3}\.\d{1, 3}\.\d{1, 3}\.\d{1, 3}|([-a-z0-9_]+\.)?[-a-z0-9]+\.[a-z]+(\.[a-z]{2, 2})?)|(www\.[a-z][-a-z0-9]+\.[a-z]+(\.[a-z]{2, 2})?))\/?[a-z0-9._\/~#&=;%+?-]+[a-z0-9\/#=?]{1, 1}/is"; 
  20. $out_count = preg_match_all($pattern, $tweet->text, $matches); 
  21.  
  22. //If there were any matches 
  23. if($out_count > 0) { 
  24. //Loop through all the full matches 
  25. foreach($matches[0] as $match) { 
  26. //Use a simple string replace to replace each URL with a HTML <a href>. 
  27. $tweet->text = str_replace($match, '<a href="'.$match.'" target="_blank" class="twitter-link">'.$match.'</a>', $tweet->text); 
  28.  
  29. $o .= "<p>".$tweet->text; 
  30.  
  31. if($r['date'] !== FALSE) { 
  32. $tweet->created_at = strtotime($tweet->created_at); 
  33.  
  34. if($r['date'] === TRUE || $r['date'] == 'true' || $r['date'] == 'TRUE' || $r['date'] == '1') { 
  35. $o .= ' - '; 
  36. } else { 
  37. $r['date'] = trim($r['date']); 
  38. $o .= " {$r['date']} "; 
  39. $o .= "<a href=\"http://twitter.com/{$username}/statuses/{$tweet->id}/\" title=\"Permalink to this tweet\" target=\"_blank\" class=\"twitter-date\">".twitter_stream_time_ago($tweet->created_at)."</a>"; 
  40.  
  41. $o .= "</p>"; 
  42.  
  43.  
  44. return array($o, $followers, $username); 
  45.