Help :: parsing RSS in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
inn
Forum Newbie
Posts: 8
Joined: Fri Dec 13, 2002 1:21 pm
Location: Malé, Maldives

Help :: parsing RSS in PHP

Post 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.
kitsch
Forum Newbie
Posts: 12
Joined: Mon Nov 11, 2002 3:29 pm

Post 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!
inn
Forum Newbie
Posts: 8
Joined: Fri Dec 13, 2002 1:21 pm
Location: Malé, Maldives

Thank you

Post 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. :)
Post Reply