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

War declared on athletics - Coe
Allegations of widespread doping in athletics are "a declaration of war" on the sport, says IAAF vice-president Lord Coe.
Date: Wed, 05 Aug 2015 07:01:11 GMT
PM 'mesmerised' by Kids Company boss
David Cameron was "mesmerised" by the Kids Company boss Camila Batmanghelidjh and overruled concerns raised, a source involved in funding talks says.
Date: Wed, 05 Aug 2015 09:52:46 GMT
Hopes fade for champion freediver
Natalia Molchanova, the 53-year-old Russian champion freediver, is feared dead after going missing on Sunday.
Date: Wed, 05 Aug 2015 09:16:06 GMT
London set for latest Tube strike
A 24-hour Tube strike is due to start at 18:30 BST following the failure to resolve a dispute between unions and London Underground.
Date: Wed, 05 Aug 2015 09:38:16 GMT
Hauliers owe £4m in migrant fines
Haulage firms and lorry drivers are fined more than £4m due to migrants found on their vehicles - with the number of fines up 50% on last year.
Date: Wed, 05 Aug 2015 07:29:32 GMT
Experts to examine 'MH370 wing part'
Experts are due to examine part of a wing that washed up on the island of Reunion last week and is thought to have belonged to missing flight MH370.
Date: Wed, 05 Aug 2015 08:36:50 GMT
India lifts porn ban after outrage
India decides to lift a blanket ban on 857 pornographic websites, following widespread outrage over the move.
Date: Wed, 05 Aug 2015 05:53:28 GMT
Burnham 'would nationalise railways'
Labour leadership contender Andy Burnham promises to renationalise Britain's railways "line by line" if he becomes prime minister.
Date: Tue, 04 Aug 2015 21:00:20 GMT
Lake Victoria collision kills two
Two children die as a passenger boat capsizes after colliding with a fishing vessel in Lake Victoria in western Kenya.
Date: Wed, 05 Aug 2015 07:02:48 GMT
Operating room music 'a distraction'
Playing music in the operating theatre can be disruptive and surgeons should think twice about pressing the play button, according to a small study.
Date: Wed, 05 Aug 2015 04:25:58 GMT

cnn

'Most dangerous' leader may have new generation of bombs
Date: Wed, 05 Aug 2015 05:09:55 EDT
2 airmen die in freefall accident
Two special tactics airmen died after a training accident at Eglin Air Force Base in Florida, the Air Force said.
Date: Wed, 05 Aug 2015 05:54:27 EDT
6 things to watch at GOP debate
Donald Trump loves the spotlight and the camera.
Date: Wed, 05 Aug 2015 06:17:22 EDT
Trump: Mexico will pay for wall because I say so
Donald Trump -- who Fox News said literally will be at the center of its GOP primary debate on Thursday -- is already feeling the heat from network hosts.
Date: Wed, 05 Aug 2015 02:28:50 EDT
Drone drops drugs into prison
Ohio prisons have had incidents with drones hovering over their yards before. But the most recent one saw the unmanned aerial vehicle become a high-flying drug mule.
Date: Tue, 04 Aug 2015 22:04:49 EDT
MH370 answers may come soon
A definitive answer on whether a barnacle-encrusted plane part that washed up on an Indian Ocean island is part of missing Malaysia Airlines Flight 370 could come this week.
Date: Wed, 05 Aug 2015 05:00:46 EDT
Anti-gay attack picks wrong men
New York police are searching for a man who randomly attacked the first male same-sex couple to be married at the United States Military Academy at West Point, authorities said Tuesday.
Date: Wed, 05 Aug 2015 06:20:47 EDT
'My heart's broken, just broken'
Wildfires like hot, dry weather. Firefighters prefer it cool and wet.
Date: Wed, 05 Aug 2015 04:16:26 EDT
Lion hunter's guide on trial
Theo Bronkhorst appears to have had a long, smooth career as an African safari hunting guide, but a tempest has engulfed him since he allegedly helped Minnesota dentist Walter Palmer kill Cecil the lion.
Date: Wed, 05 Aug 2015 05:36:35 EDT
27 die in train derailment
At least 27 have died and many more are injured after two trains veered off a flooded bridge in central India, local authorities said.
Date: Wed, 05 Aug 2015 03:24:40 EDT

flickr

20070512-P1030643.jpg

julienlavergne posted a photo:

20070512-P1030643.jpg

Date: 2015-08-05T10:25:29Z
Когда ты показывал лося, а нарисовали медведя😂😂😂 А если серьезно, то всем приятного аппетита и отличного дня))) среда -середина недели , скоро выходные 😂😂😂☺️☺️☺️ #поднимательнастроения #селфимания #вдушеяклоун

ivan_draga1 posted a photo:

Когда ты показывал лося, а нарисовали медведя😂😂😂 А если серьезно, то всем приятного аппетита и отличного дня))) среда -середина недели , скоро выходные 😂😂😂☺️☺️☺️ #поднимательнастроения #селфимания #вдушеяклоун

by taximanius ift.tt/1IkdQTB

Date: 2015-08-05T10:25:31Z
DSC_0562.jpg

MartinKalina posted a photo:

DSC_0562.jpg

Date: 2015-08-05T10:25:31Z
"Ювентус" намерен арендовать Сикейру

Smotrisport posted a photo:

"Ювентус" намерен арендовать Сикейру

Узнать больше на сайте СмотриСпорт: ift.tt/1IMiqvx

Date: 2015-08-05T10:25:33Z
Rocciamelone 2015 da Malciaussia

Carlo Ferrero posted a photo:

Rocciamelone 2015 da Malciaussia

Date: 2015-08-05T10:25:31Z
DSC00017

Steve Hayes posted a photo:

DSC00017

Date: 2015-08-05T10:25:31Z
TDL-33

Yuukidisney posted a photo:

TDL-33

Date: 2015-08-05T10:25:32Z
Finally 💃💃😿

charliecun posted a photo:

Finally 💃💃😿

by nada_altwaijry ift.tt/1IMitau

Date: 2015-08-05T10:25:29Z
Summercamp2015_3traininng_324

Federación Kyokushinkai España :: Shinkyokushin posted a photo:

Summercamp2015_3traininng_324

Date: 2015-08-05T10:25:33Z
Rock Werchter 2015: Sfeer

stubru posted a photo:

Rock Werchter 2015: Sfeer

Date: 2015-08-05T10:25:29Z

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 'CSS' here:

BB code available :

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