Location: PHPKode > scripts > RSS-functions > rss-functions/inc_functions_rss.php
<?

// INPUT	: [string] URL of de RSS-feed which should be translated to an array
// OUTPUT	: [array] 2D-Array with all the data
//			  $array[x]['title'] gives the title of message message x+1 etc. etc.
function RSStoArray($feed) {
	$content = file_get_contents ($feed);	
	$items = explode ('<item>', $content);
	array_shift($items);
	$i = 0;
	
	foreach ($items as $item) {
		$array[$i]['title']			= getTextBetweenTags($item, 'title');
		$array[$i]['link']			= getTextBetweenTags($item, 'link');
		$array[$i]['description']	= getTextBetweenTags($item, 'description');
		$array[$i]['author'] 		= getTextBetweenTags($item, 'author');
		$array[$i]['category'] 		= getTextBetweenTags($item, 'category');
		$array[$i]['comments'] 		= getTextBetweenTags($item, 'comments');
		$array[$i]['enclosure'] 	= getTextBetweenTags($item, 'enclosure');
		$array[$i]['guid'] 			= getTextBetweenTags($item, 'guid');
		$array[$i]['pubDate'] 		= getTextBetweenTags($item, 'pubDate');
		$array[$i]['source'] 		= getTextBetweenTags($item, 'source');
		$i++;
	}
	
	return $array;
}


// INPUT	: [string] Text in which should be searched
//			  [string] Name of the tag WITHOUT < and > !
// OUTPUT	: [string] Text between the tags
function getTextBetweenTags($text, $tag) {
	$StartTag = "<$tag";
	$EndTag	= "</$tag";
	
	$StartPosTemp = strpos($text, $StartTag);
	$StartPos = strpos($text, '>', $StartPosTemp);
	$StartPos = $StartPos + 1;
	
	$EndPos = strpos($text, $EndTag);
	
	if($EndPos > $BeginPos)	{
		$text	= substr ($text, $StartPos, ($EndPos - $StartPos));
	} else {
		$text = '';
	}
	
	$text = str_replace('<![CDATA[', '', $text);
	$text = str_replace(']]>', '', $text);
	
	return trim($text);
}


// INPUT	: [array] Array obtained with 'RSStoArray'
//			  [integer] Maximal number of headlines
//			  [string] Type of layout
// OUTPUT	: [string] Formatted HTML-code 
function ArrayToHTML($array, $aantal = '', $type = '') {
	$HTML = "";
	
	if($aantal != '') {
		$RSS = array_slice ($array, 0, $aantal);
	} else {
		$RSS = $array;
	}
	
	if($type == 'news') {
		foreach ($RSS as $item) {
			$HTML .= "<a href='$item[link]' target='_blank' title='".  preg_replace("/(\r\n|\n|\r)/", "", strip_tags($item[description])) ."'>$item[title]</a><br>\n";
		}	
	} else {
		foreach ($RSS as $item) {
			foreach ($item as $key => $value) {
				if($value != "") {			
					$HTML .= "<b>$key</b><br>\n";				
					if($key == 'link') {
						$HTML .= "<a href='$value' target='_blank'>$value</a>\n";	
					} else {
						$HTML .= "$value\n";
					}
					$HTML .= "<p>\n";
				}
			}		
			$HTML .= "<hr>";
		}
	}
		
	return $HTML;
}

?>
Return current item: RSS-functions