Back to news

Source-code to RSS parser

<?php

	/* Oliver White, 2002.  Written in the PHP scripting language */

	/* Display a given RSS newsfeed */
	function ShowRSS($URL)
	{		
		/* Open a connection to the website, and get the news file */
		$Data = implode('', file($URL));

		/* Search for information about the newsfeed itself */
		$NumItems = preg_match('|<channel.*?>.*?<title>(.*?)</title>.*?<link>(.*?)</link>.*?<description>(.*?)</description>.*?</channel>|s', $Data, $Notes);

		/* Parse into variables, and display a title */
		$Title = $Notes[1];
		$URL = $Notes[2];
		$Description = $Notes[3];
		print "<h2><a href=\"$URL\">$Title</a></h2>\n";
		
		/* Find all news items using a regular expression */
		$NumItems = preg_match_all ('|<item.*?>.*?<title>(.*?)</title>.*?<link>(.*?)</link>.*?<description>(.*?)</description>.*?</item>|s', $Data, $Items);

		/* If no items found, try again without the description field (slashdot-compatibility) */
		if( $NumItems == 0)
		{
			$NumItems = preg_match_all ('|<item.*?>.*?<title>(.*?)</title>.*?<link>(.*?)</link>.*?</item>|s', $Data, $Items);
		}

		print "<table border=\"0\" cellpadding=\"7\" width=\"60%\">\n";

		/* Loop through all news items found */
		for( $j=0 ; $j < $NumItems ; $j++)
		{
			$Title = $Items[1][$j];
			$URL = $Items[2][$j];
			$Description = $Items[3][$j];
			
			/* Display the item */
			print "\t<tr><td valign=\"top\"><a href=\"$URL\">$Title</a></td><td>$Description</td></tr>\n";
		}			
		print "</table>\n\n";
	}
	
	/* Hide news if view-source mode is on */
	if(! $ViewSource )
	{
	
		/* display internetnews.com junk */
		
		//ShowRSS("");
		
		ShowRSS("http://www.theregister.co.uk/tonys/slashdot.rdf");
		ShowRSS("http://slashdot.org/slashdot.rss");
		
		ShowRSS("http://www.webreference.com/webreference.rdf");
		ShowRSS("http://www.wired.com/news_drop/netcenter/netcenter.rdf");
				
		ShowRSS("http://headlines.internet.com/internetnews/bus-news/news.rss");
		ShowRSS("http://headlines.internet.com/internetnews/wd-news/news.rss");

	}
?>

<?php
	/* Option to view source-code */
	if( $ViewSource )
	{
		print "<div class=\"source\">";
		highlight_file("index.php");
		print "</div>";
		
		/* Link to view-result */
		print "<p><a href=\"./\">View result</a></p>\n";

	}
	else /* Link to view-source */
	{
		print "<p><a href=\"./?ViewSource=1\">View source-code of this page</a></p>\n";
		
	}
	
?>

Back to news