Location: PHPKode > scripts > RSS parser > rss.class.php
<?

#######################=-##=-=##
#                              #
#  RSS parse engine v 1.0      #
#                              #
#  author: hide@address.com  #
#                              #
##=-##=-=#######################




// Additional function for templates


function __tpl($tpl,$array){
 if(is_array($array) && $tpl){
    foreach($array as $key=>$val) $tpl = str_replace("%$key%",$val,$tpl);
    return $tpl;
 }
}





// Rss parse class


class Rss{

	var $Count=0;
        var $Template;

	var $Items=array();

	var $CashPath="";


	function rss($url){

		global $Items;

                if(!$url) die("RSS: url don't exists"); 
			

		$urlx=parse_url($url);
		
		$Filename=$this->CashPath.$urlx[host].".rss";


		$modifed=time()-@filemtime($Filename);




		if(!file_exists($Filename) || $modifed>CASHE_TIMEOUT){
		
			if( !($content = file_get_contents($url)) ) die("RSS: sourse error");


			
			$rss_tmp=fopen($Filename,"w");

			fputs($rss_tmp,$content);

				 fclose($rss_tmp);
			 
		}

		else $content = file_get_contents($Filename);


		


		preg_match_all("/<item>(.+)<\/item>/Uis",$content,$Items1,PREG_SET_ORDER);


		foreach($Items1 as $indx=>$var){

			$this->Items[$indx]=$var[1];

		}
	

	}



	function parseItems(){	

		$Item=$this->Items[$this->Count];

		if(!$Item) return FALSE;		

		$this->Count++;

		preg_match_all("/<(title|link|description)>(.+)<\/(\\1)>/is",$Item,$ParsedItem,PREG_SET_ORDER);
		

		$ParsedArray[title]	  = htmlspecialchars($ParsedItem[0][2],ENT_QUOTES);

		$ParsedArray[link]	  = urldecode($ParsedItem[1][2]);

		$ParsedArray[description] = htmlspecialchars($ParsedItem[2][2],ENT_QUOTES);
		

		return __tpl($this->Template,$ParsedArray);


	}	



}


?>
Return current item: RSS parser