RSS/XML feed parser

Here's some php:

PHP
function xml_parser($page,$container,$tags,$number,$cdata) {
  if (!$number) {$number=100;}
  $stories=0;
  $xml=file_get_contents($page);
  preg_match_all("/<$container>.+<\/$container>/sU",$xml, $items);
  $items=$items[0];
  $itemsArray=array();
   foreach ($items as $item) {
    for($i=0; $i<count($tags); $i++) {
    preg_match("/<$tags[$i](.+)(<\/$tags[$i]>)/sU", $item, $tag);
    $this[$i]=preg_replace("/<$tags[$i]>(.+)(<\/$tags[$i]>)/sU",'$1',$tag);
    $this[$i]=array_map('html_entity_decode', $this[$i]);
    }
     if (count($itemsArray)<$number) {array_push($itemsArray, $this);}
   }
  $theData="<dl>";
  foreach ($itemsArray as $item) {
  for($i=0; $i<count($tags); $i++) {
  $data[$i]=$item[$i][0];    }
   $title=$data[0];
   $dpatterns[0]="/<img(.+)><\/img>/sU"; $dreplacements[0]='<img$1>';
   $dpatterns[1]="/<img(.+)\/>/sU"; $dreplacements[1]='<img$1>';
   $dpatterns[2]="/<(\/|)content?(.+|)>/sU"; $dreplacements[2]='';
   $dpatterns[3]="/border=\"0\"/sU"; $dreplacements[3]='';
   if ($cdata!='hide') {
    $dpatterns[4]="/<\!\[CDATA\[(.+)\]\]>/sU"; $dreplacements[4]='$1';
   }
   else {
    $dpatterns[4]="/<\!\[CDATA\[(.+)\]\]>/sU"; $dreplacements[4]='';
   }
   $description=preg_replace($dpatterns,$dreplacements,$data[1]);
   $link=preg_replace("/<link.+href=\"(.+)\"(.+|)\/>/sU",'$1',$data[2]);
   $date=$data[3];
   $theData.="
   <dt><a href=\"$link\">$title</a></dt>
   <dd class=\"story\">$description</dd>
   <dd>Date: $date</dd>\r";
  }
$theData.="</dl>";
return $theData;
}

$container='item';
$tags=array('title','description','link','pubDate');
$bbc=xml_parser("http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml",$container,$tags,10,'');
$cnn=xml_parser("http://rss.cnn.com/rss/cnn_topstories.rss",$container,$tags,10,'');

$container='entry';
$tags=array('title','content','link','published');
$flickr=xml_parser("http://api.flickr.com/services/feeds/photos_public.gne",$container,$tags,10,'');

Here's some HTML with PHP

HTML/PHP
<h2>bbc</h2>
<?php echo $bbc; ?>
<h2>cnn</h2>
<?php echo $cnn; ?>
<h2>flickr</h2>
<?php echo $flickr; ?>

Here's what we get... (the lastest feeds from the BBC, CNN, and flickr).

bbc

At least 159 people have been killed and more than 360 injured in a 6.2-magnitude quake in central Italy, officials say.
Date: Wed, 24 Aug 2016 22:00:42 GMT
Two more bodies are pulled from the sea at Camber Sands in East Sussex, hours after three men were recovered from the water on the hottest day of the year.
Date: Wed, 24 Aug 2016 21:32:51 GMT
The family of a British backpacker stabbed to death in Australia describe her as an "amazing young woman with an adventurous spirit".
Date: Wed, 24 Aug 2016 19:29:57 GMT
A serving Royal Marine arrested over Northern Ireland-related terrorism is Ciaran Maxwell from County Antrim
Date: Wed, 24 Aug 2016 21:46:38 GMT
Scientists identify a planet only slightly larger than Earth that is orbiting Proxima Centauri - the closest star to our Solar System.
Date: Wed, 24 Aug 2016 17:00:52 GMT
The world's longest aircraft - the Airlander 10 - is damaged when it takes a nosedive on landing during its second test flight.
Date: Wed, 24 Aug 2016 16:23:06 GMT
Labour leader Jeremy Corbyn reacts angrily to questions about whether he ignored empty seats before filming a piece about overcrowded trains.
Date: Wed, 24 Aug 2016 16:01:10 GMT
Outgoing UKIP leader Nigel Farage is to address supporters of Republican presidential nominee Donald Trump at a campaign event in Mississippi.
Date: Wed, 24 Aug 2016 21:30:20 GMT
The UK records its hottest temperature of the year so far - 33.9C (93F) at Gravesend in Kent - as experts warn of possible health risks.
Date: Wed, 24 Aug 2016 19:41:55 GMT
A Vietnamese woman admits to paying for her left hand and foot to be severed to defraud her insurance firm with a rail accident claim, police say.
Date: Wed, 24 Aug 2016 15:24:23 GMT

cnn

The 6.2-magnitude earthquake badly damaged villages in a mountainous rural area of central Italy, a popular vacation spot.
Date: Wed, 24 Aug 2016 22:26:14 GMT
People are trapped at the American University of Afghanistan in Kabul after reports of gunfire and blasts.
Date: Wed, 24 Aug 2016 17:10:43 GMT
A blast and gunshots have been heard at the American University of Afghanistan complex in Kabul, according to eyewitnesses. Fires are burning on the campus, said Tarek Mahjari, a reporter for Tolo News who reported the details in a phone interview with his employer. Ambulances, security forces and search and rescue teams are en route to the university. The blast occurred at 7:50 p.m. local, when students were gathering and eating together. Officials tell CNN that it is too early to comment.
Date: Wed, 24 Aug 2016 22:19:17 GMT
At a rally in Tampa, Florida, Donald Trump criticized celebrity supporters of Hillary Clinton calling them "celebrities that aren't very hot anymore."
Date: Wed, 24 Aug 2016 19:15:40 GMT
Donald Trump and Hillary Clinton are neck and neck in North Carolina, a state solidifying its position as a perennial presidential election battleground, while Trump holds a 5-point lead in the traditionally GOP-tilting state of Arizona, according to new CNN/ORC battleground state polls.
Date: Wed, 24 Aug 2016 22:30:01 GMT
A planet named Proxima B has been discovered orbiting the closest star to our Sun. It may be the closest possible home for life outside of the solar system.
Date: Wed, 24 Aug 2016 17:46:34 GMT
Don Lincoln says the finding of a relatively close by exoplanet is exciting, but the chances that life exist there are somewhat slim.
Date: Wed, 24 Aug 2016 19:57:25 GMT
A 6.2-magnitude quake hit central Italy killing at least 37 people. Witnesses describe the devastation.
Date: Wed, 24 Aug 2016 15:02:06 GMT
A 6.2-magnitude earthquake hit southeast of Norcia, Italy, according to the United States Geological Survey.
Date: Wed, 24 Aug 2016 18:24:46 GMT
CNN correspondent Barbie Nadeau was nearing the end of a Facebook Live Wednesday afternoon in Saletto, Italy, answering a viewer's question about the condition of the city's roads, when a building collapsed directly behind her.
Date: Wed, 24 Aug 2016 17:28:57 GMT

flickr

050816_1360_mallory_park.jpg

peterwilemanphotography posted a photo:

050816_1360_mallory_park.jpg

05-08-2016 Mallory Park trackday photographs

Date: 2016-08-24T23:06:00Z
Quick acrylic on canvas sketch . Love the girls expression #johnnyarmstrongdesigns #artbasel #picoftheday #artista #artgallery #acrylicpainting #miamibeach #artwork #painting

kevindusoe posted a photo:

Quick acrylic on canvas sketch . Love the girls expression #johnnyarmstrongdesigns #artbasel #picoftheday #artista #artgallery #acrylicpainting #miamibeach #artwork #painting

Date: 2016-08-24T23:06:00Z
_JUC0568.jpg

JacsPhotoArt posted a photo:

_JUC0568.jpg

Date: 2016-08-24T23:06:05Z
293

jlgriffith13 posted a photo:

293

Date: 2016-08-24T23:06:03Z
CCTV

gotbadger posted a photo:

CCTV

via Instagram ift.tt/2bxssXD

Date: 2016-08-24T23:05:58Z
DSCF0755

卡妞 posted a photo:

DSCF0755

Date: 2016-08-24T23:05:58Z
Sunset

Joe Vio posted a photo:

Sunset

Off Santorrini

Date: 2016-08-24T23:05:59Z
IMG_0526.jpg

Stephen D Harper posted a photo:

IMG_0526.jpg

Date: 2016-08-24T23:06:03Z
#Edinburgh #northbridge #scotland #hometown stopping to take in the #perspective #view #contrast #city #citiesoftheworld

ciaran-frame posted a photo:

#Edinburgh #northbridge #scotland #hometown stopping to take in the #perspective #view #contrast #city #citiesoftheworld

Date: 2016-08-24T23:06:04Z
Skate

François LLORENS posted a photo:

Skate

Date: 2016-08-24T23:05:59Z

Comments

#1
2007-03-02 dumb_dave says :

Sorry, I'm new to this stuff, willing to learn and all that, but I don't get the idea. Copy that snippet of PHP code into a file and call it, say, parser.php. Copy the other snippet of HTML into a file and call it, for lack of inventiveness, parser.html. Right so far? If so, where's the intermediate step? How does this HTML "call" or "include" the PHP in order to function? Or am I missing something so basic that even asking this will earn me the cherished "Idiot of the Day Award"? Thanks.

#2
2007-03-02 BonRouge says :

dave,
You can include the php or just have it in one page. The page would have a '.php' extension - not '.html.'
Here's a simple example of this page (with no style or anthing) in one file.
Save it and change the extension to '.php'. If you don't have a server installed on your machine, you'll have to upload it to a remote server to view it.
If you want, you can take the php code out of that page and save it in a different file and include it into the page - that way, you could use it on more than one page if you wanted.

I hope that makes it a bit clearer.

#3
2007-03-02 dumb_dave says :

Thanks for the explanations. Much clearer now and ... yes, it indeed works like a champ. (Maybe I was just too tired? Putting 1 and 1 together and coming up with 11 instead of two?) Best regards and thanks for all the tips elsewhere as well.

#4
2007-03-07 dumb_dave says :

Useful indeed, BonRouge, but how does one display the <description> tagged material that is buried behind things like <![CDATA[ <p> etc.? Is the PHP code easily modified to handle that? And if so, can one apply it selectively? That is, show the fuller "description" material for one site but then reduce the next site entry to "headines" only (i.e., "titles" and "links") and then toggle the next one back to fuller details? Hope this is not a major headache, but it's beyond my ability to work it out at this stage ... and everything tried brought the larger process to a grinding halt. (This isn't a do-my-homework-for-me question. I'm bewildered by the code.) Thanks.

#5
2007-03-07 BonRouge says :

dave,
I thought I'd already sorted out the problem of data wrapped in the CDATA stuff. Does the code have a problem? If you could show me where it's not working, I'll try to improve it.
As for choosing whether to show that particular data or not, yes - I think you could do that by adding another variable. You see near the top where there's a preg_replace() to remove the CDATA tags? You could put that in an if statement - if the variable is not present, remove the CDATA tags, if it is, leave them where they are.
Does that make sense?

#6
2007-03-10 BonRouge says :

dave,
I think I found the problem and sorted it out. As you can see, it seems to work OK now. Some of the characters in the Lockergnome feed don't show right on this page though. I wonder if it's anything to do with me being in Japan. Do you see strange characters?

#7
2007-05-01 Ice says :

I have been trawling the web for days looking for something like this. Thanks a WHOLE lot man. I was also wondering if you can modify this parser to merge these fields and display, say, only the latest 10 items? wine

#8
2007-11-02 steve says :

thanks sorted out my cdata parasing problem, seems that is not too clear in the docs

s

Comment form

Please type the word 'life' here:

BB code available :

  • [b]...[/b] : bold
  • [it]...[/it] : italic
  • [q]...[/q] : quote
  • [c]...[/c] : code
  • [url=...]...[/url] : url