Twitter Feed anzeigen

Folgendes kleine Script liest die Feeds eines Twitter Accounts aus und gibt diese in einer Liste aus:

twitterfeed.php
<?php
	$twitterfeed = 'http://twitter.com/statuses/user_timeline/15350737.rss';
 
	// Load xml file
	if(@$xml = simplexml_load_file($twitterfeed, 'SimpleXMLElement', LIBXML_NOCDATA)) {
		echo '<ul>';
 
		// Iterate throw items
		foreach ($xml->channel->item as $item) {
			// Get the attributes
			$date = date("d.m.Y H:i:s", strtotime($item->pubDate));
			$title = utf8_decode($item->title);
			$link = $item->link;
 
			// Write out the tweet
			echo '<li>';
				echo '<span class="date">'.$date.'</span>';
				echo '<span class="link"><a href="'.$link.'" title="[tweet]" target="_blank" rel="nofollow">[tweet]</a></span>';
				echo '<span class="title">'.$title.' </span>';
			echo '</li>';
		}
 
		echo '</ul>';
	}
?>

Folgende beiden Methoden verlinken die im Tweet vorhandenen User und Links:

functions.php
<?php
	function hyperlinks($text) {
		// match protocol://address/path/file.extension?some=variable&another=asf%. by Allen Shaw & webmancers.com
		$text = preg_replace('/\b([a-zA-Z]+:\/\/[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/i',"<a href=\"$1\" class=\"twitter-link\" target=\"_blank\" rel=\"nofollow\">$1</a>", $text);
 
		// match www.something.domain/path/file.extension?some=variable&another=asf%
		$text = preg_replace('/\b(?<!:\/\/)(www\.[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/i',"<a href=\"http://$1\" class=\"twitter-link\" target=\"_blank\" rel=\"nofollow\">$1</a>", $text);    
 
		// match name@address
		$text = preg_replace("/\b([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]*\@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})\b/i","<a href=\"mailto://$1\" class=\"twitter-link\" target=\"_blank\" rel=\"nofollow\">$1</a>", $text);
 
		//mach #trendingtopics. by Michael Voigt
		$text = preg_replace('/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)#{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', "$1<a href=\"http://twitter.com/#search?q=$2\" class=\"twitter-link\" target=\"_blank\" rel=\"nofollow\">#$2</a>$3 ", $text);
		return $text;
	}
 
	function twitter_users($text) {
		   $text = preg_replace('/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', "$1<a href=\"http://twitter.com/$2\" class=\"twitter-user\" target=\"_blank\" rel=\"nofollow\">@$2</a>$3 ", $text);
		   return $text;
	} 
?>

Um dies anzuwenden einfach den Titel durch die Funktionen lassen:

$title = twitter_users(hyperlinks(htmlentities($title)));
php/twitter_feed_anzeigen.txt · Zuletzt geändert: 27.12.2013 19:46 (Externe Bearbeitung)
Nach oben
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0