setLimit(5); $feed = $RSS->getRSS('http://rss.slashdot.org/Slashdot/slashdot'); $meta = $RSS->getChannel(); echo "

{$meta['title']}

{$meta['description']}

"; for($i = 0; $i < count($feed); $i++){ echo "{$feed[$i]['title']}
"; } echo '
';
print_r($feed);
print_r($meta);
echo '
'; ?> */ class RSSReader { // Parsing var $default_encoding = 'utf-8'; var $rss_content = ''; var $use_tags = 0; var $limit = 0; var $content = array(); var $channel_tags = array('title', 'link', 'description', 'language','pubDate', 'lastBuildDate', 'docs', 'generator','managinEditor','webMaster','copyright','ttl' ); var $item_tags = array('title','link','description','author','category', 'comments','guid', 'pubDate','source' ); // Cache var $use_cache = 1; var $cache_time = 1200; var $cache_dir = './cache/'; var $cache_ext = 'cache'; /** * @access public * @param $url * @return feed in array */ function getRSS($url){ if(($this->use_cache == 1) && (is_dir($this->cache_dir))){ $cachefile = $this->cache_dir . md5($url) . '.' . $this->cache_ext; $cachefile_created = file_exists($cachefile) ? filemtime($cachefile) : 0; clearstatcache(); if (time() - $this->cache_time < $cachefile_created) { $this->getRSSFile($cachefile); }else{ if ($f = fopen($cachefile, 'w')) { fwrite ($f, $this->getRSSFile($url)); fclose($f); } } }else{ $this->getRSSFile($url); } return $this->parseFile(); } /** * @access public * @param none * @return channel tags in array */ function getChannel(){ $retval = array(); preg_match("'(.*?)'si", $this->rss_content, $channels); foreach($this->channel_tags as $tag){ preg_match("'<$tag>(.*?)'si", $channels[1], $node[$v]); $retval[$tag] = $node[$v][1]; } return $retval; } /** * @access private * @param none * @return parsed rss-file */ function parseFile(){ // Strip CDATA $this->rss_content = strtr($this->rss_content, array(''', ']]>'=>'')); if(function_exists('iconv')){ // Detect encoding or fall back to default preg_match("'encoding=[\'\"](.*?)[\'\"]'si", $this->rss_content, $encoding); if($encoding[1] != ''){ $this->rss_content = iconv($encoding[1], 'utf-8//TRANSLIT', $this->rss_content); }else{ $this->rss_content = iconv($this->default_encoding, 'utf-8//TRANSLIT', $this->rss_content); } } // Grab all items to array preg_match_all("'(.*?)'si", $this->rss_content, $items); if((count($items[0]) < $this->limit) || ($this->limit == 0)){ $this->limit = count($items[0]); } for($i = 0; $i < $this->limit; $i++){ foreach($this->item_tags as $tag){ preg_match("'<$tag.*?>(.*?)'si", $items[0][$i], $node[$i]); $this->content[$i][$tag] .= $this->transformEntities($node[$i][1]); } } return $this->content; } /** * @access private * @param $s String * @return html translated string */ function transformEntities($s){ $a = array_flip(get_html_translation_table (HTML_ENTITIES, ENT_QUOTES)); if($this->use_tags == 1){ $rv = strtr($s, $a); }else{ $rv = strip_tags(strtr($s, $a)); } return $rv; } /** * @access private * @param $rss_file * @return rss-file */ function getRSSFile($rss_file){ if ($f = @fopen($rss_file, 'r')) { while (!feof($f)) { $this->rss_content .= fgets($f, 4096); } fclose($f); } return $this->rss_content; } // Public setter methods function setLimit($i = 0){ $this->limit = $i; } function setUseTags($i = 0){ $this->use_tags = $i; } function setCacheExt($str = 'cache'){ $this->cache_ext = $str; } function setCacheDir($str = './cache/'){ $this->cache_dir = $str; } function setUseCache($i = 1){ $this->use_cache = $i; } function setCacheTime($i = 1200){ $this->cache_time = $i; } } ?>