Page 1 of 1

Help :: parsing RSS in PHP

Posted: Fri Dec 13, 2002 1:21 pm
by inn
hi. i need help in parsing and/or reading a rss document. as you know rss is used around alot of websites to provide information to other sites as news and other data. its an xml file but i've tried parsing an rss document and couldn't make. but a similar version of the same document in xml can be parsed much more conveniently.

please provide some help and a sample script if possible.

Posted: Fri Dec 13, 2002 4:40 pm
by kitsch
Sample script:

Code: Select all

function rdf2array($filename) {
	$file = @file($filename);
	if($file == "") {
		$element_arrayї0] = "http://www.somelink.net";
		$element_arrayї1] = "error opening the newssource...";
		$news_arrayї] = $element_array;
		return $news_array;
	}
	
	$element_beg = "<item>|<story>";
	$element_end = "</item>|</story>";
	$channel_end = "</rss>|</rdf:RDF>|</backslash>";
	$element_tit = "<title>";
	$element_dat = "<time>";
	$element_des = "<description>";
	$element_url = "<link>|<url>";

	while(list (, $line) = each($file)) &#123;
		if(ereg($channel_end,  $line)) break;
		if(ereg($element_end,  $line)) continue;
		if(!ereg($element_beg, $line)) continue;

		while(list (, $line) = each($file)) &#123; 
			$line = trim($line);
			if ($line == "") continue;
			if ($line&#1111;0] != "<") continue;
			if (ereg($element_end, $line)) break;
			
			switch (true) &#123;
				case ereg($element_url, $line) :
					$url = trim(strip_tags($line));     
					break;
				case ereg($element_tit, $line) :
					$title = trim(strip_tags($line));     
					break;
				case ereg($element_des, $line) :
					$description = trim(strip_tags($line));     
					break;
				case ereg($element_dat, $line) :
					$date = trim(strip_tags($line));
					break;
			&#125;
		&#125;

		$element_array = Array();
		$element_array&#1111;0] = $url;
		$element_array&#1111;1] = $title;
		$element_array&#1111;2] = $description;
		$element_array&#1111;3] = $date;

		$news_array&#1111;] = $element_array;
	&#125;
	return $news_array;
&#125;

$news_array = rdf2array('http://www.newsprovider.com/lalala/');
foreach($news_array as $news_pack) &#123;
	list($url, $title, $description, $date) = $news_pack;
	echo("<a href='$url'>$title</a> - $description - $date<br>");
&#125;
This should work!

Thank you

Posted: Sat Dec 14, 2002 8:50 am
by inn
thank you kitsch for the sample script. i'll try to work it out. anyway i've already come up with a script of mine own to parse the file. i've used expat. xml php to parse the file and i've worked it out. if you'd like to see the result of the script when it parses the rss file from http://www.phparch.com/phpa.rss, its here http://www.qbin.org/inn/xmlparser.php.

once again thank you. :)