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,'');

$tags=array('title','content:encoded','link','pubDate');
$lockergnome=xml_parser("http://feed.lockergnome.com/nexus/all",$container,$tags,5,'hide');

$tags=array('title','content:encoded','link','pubDate');
$lockergnome=xml_parser("http://feed.lockergnome.com/nexus/all",$container,$tags,5,'');

$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>lockergnome</h2>
<?php echo $lockergnome1; ?>
<h2>lockergnome</h2>
<?php echo $lockergnome2; ?>
<h2>flickr</h2>
<?php echo $flickr; ?>

Here's what we get... (the lastest feeds from the BBC, CNN, Lockergnome - with CDATA stripped and shown - and flickr).

bbc

May: Our values will beat extremists
A future Conservative government would seek new powers to ban extremist groups and curb the activities of "harmful" individuals, Theresa May says.
Date: Tue, 30 Sep 2014 14:36:42 GMT
PM vows seven-day GP access by 2020
David Cameron announces plans to ensure seven-day GP access is rolled out across England by 2020.
Date: Tue, 30 Sep 2014 07:51:29 GMT
Dead teacher filmed boys changing
Indecent images of pupils are found on the computer of an Essex deputy head teacher who was found dead after he was visited by police.
Date: Tue, 30 Sep 2014 13:51:07 GMT
Kurds fight IS on Iraq-Syria border
Heavy fighting is reported on both sides of a key border crossing between Iraq and Syria, where Kurdish forces are battling Islamic State militants.
Date: Tue, 30 Sep 2014 14:14:07 GMT
Hong Kong leader in protest appeal
Hong Kong leader CY Leung urges pro-democracy protesters to "immediately" stop their campaign, as crowds bring areas to a standstill.
Date: Tue, 30 Sep 2014 12:43:42 GMT
eBay to split off PayPal business
E-commerce site eBay is planning to split off its payments system PayPal into a separate company next year.
Date: Tue, 30 Sep 2014 15:11:59 GMT
White House security plan 'failed'
The director of the US Secret Service says its security plan was "not properly executed" when a man broke into the White House this month.
Date: Tue, 30 Sep 2014 15:47:01 GMT
Boy's life-support should end - judge
A boy with irreversible brain damage should be taken off a life-support machine despite the objections of his parents, a High Court judge rules.
Date: Tue, 30 Sep 2014 12:32:56 GMT
Afghans sign deal to keep US troops
The new Afghan government signs a security deal with American officials that will allow US troops to remain in the country beyond 2014.
Date: Tue, 30 Sep 2014 12:35:16 GMT
UK second quarter growth revised up
UK economic growth has been revised up to 0.9% for the second quarter of the year by the Office for National Statistics.
Date: Tue, 30 Sep 2014 10:27:10 GMT

cnn

Walmart responds to Tracy Morgan suit
Walmart has responded to a lawsuit filed by Tracy Morgan over a deadly car crash that left the comedian in critical condition. The big box store blamed the injuries suffered by Morgan on his failure to wear a seat belt.
Date: Tue, 30 Sep 2014 07:57:41 EDT
Gates praises U.S. response to Ebola
One of the world's most successful innovators and philanthropists defended the U.S. response to the deadly Ebola epidemic Monday, saying he doesn't think the government waited too long to act.
Date: Tue, 30 Sep 2014 08:04:13 EDT
Bleach is 'best friend' in Ebola fight
CNN's Nima Elbagir reflects on a harrowing trip to Liberia where she covered the deadliest Ebola outbreak in history.
Date: Tue, 30 Sep 2014 10:42:30 EDT
Boehner: Maybe we'll have Syria debate
Just days after suggesting lawmakers might not debate U.S. military involvement in Syria until next year, House Speaker John Boehner said he'd agree to call Congress back into session to vote if President Barack Obama sends up a resolution asking for the authority.
Date: Mon, 29 Sep 2014 22:35:51 EDT
8 arrested in Ferguson
Eight people were arrested outside the Ferguson, Missouri, Police Department on Sunday night when demonstrations grew violent yet again, police say.
Date: Tue, 30 Sep 2014 06:12:06 EDT
Bill targets fed workers' porn habit
Rep. Mark Meadows talks about a bill that he introduced to ban federal employees from watching pornography at work.
Date: Tue, 30 Sep 2014 10:46:56 EDT
Air traffic control fire suspect in court
A contract employee accused of setting a fire at a key Chicago air traffic control center on Monday made his first court appearance since the incident created flight problems that rippled across the nation.
Date: Tue, 30 Sep 2014 04:09:20 EDT
Ben Bradlee is in hospice care
Ben Bradlee, the former top editor of The Washington Post who oversaw the paper's coverage of the Watergate scandal, is in hospice care as his health has declined over the last six weeks, his wife said in a C-SPAN interview.
Date: Tue, 30 Sep 2014 06:52:32 EDT
Smart toothbrush tracks brushing
This Bluetooth enabled toothbrush coaches you while you brush and tracks your progress through a smartphone app.
Date: Tue, 30 Sep 2014 10:38:43 EDT
Clinton granddaughter gets presents
New grandmother Hillary Clinton was presented with a slew of gifts, mostly books, for her baby granddaughter, Charlotte Clinton Mezvinsky, Monday afternoon at a private New York City fundraiser for the Democratic Congressional Campaign Committee.
Date: Mon, 29 Sep 2014 21:20:43 EDT

lockergnome (hidden CDATA)

The lockergnome feed seems to be down.

lockergnome

The lockergnome feed seems to be down.

flickr

Как же я люблю осень

ivan_draga1 posted a photo:

Как же я люблю осень

by apollo_mm ift.tt/1uyT65V

Date: 2014-09-30T15:49:46Z
Shop http://t.co/mPeHQmC8We for that @Obedbags @noblerebels backpack n get a T-shirt,skateboard,a hat n stickers. http://t.co/w6lfS8SVOw Wx

Walking Witness posted a photo:

Shop http://t.co/mPeHQmC8We for that @Obedbags @noblerebels backpack n get a T-shirt,skateboard,a hat n stickers. http://t.co/w6lfS8SVOw Wx

ift.tt/1wUDUhJ WalkingWitness September 30, 2014 at 04:42PM

Date: 2014-09-30T15:49:50Z
IMG_3156

Ana Paula Hirama posted a photo:

IMG_3156

Date: 2014-09-30T15:49:43Z
Start of a sketch this morning. A girl named June from 1949.

GallivantingGirl posted a photo:

Start of a sketch this morning. A girl named June from 1949.

Date: 2014-09-30T15:49:47Z
うまかったヨーグルト

masa_grant55 posted a photo:

うまかったヨーグルト

Date: 2014-09-30T15:49:47Z
MMR's new pens. Come get one at #pack2014 and chat about your research needs.

mmr-research posted a photo:

MMR's new pens. Come get one at #pack2014 and chat about your research needs.

via Instagram ift.tt/10kpr3Q

Date: 2014-09-30T15:49:48Z
IMG_0524

manfredj72 posted a photo:

IMG_0524

Date: 2014-09-30T15:49:49Z
Before After Facelift With Botox Injections Of Julianna Margulies Plastic Surgery

jchigley posted a photo:

Before After Facelift With Botox Injections Of Julianna Margulies Plastic Surgery



Celebrity Plastic Surgery PhotoS has published Before After Facelift With Botox Injections Of Julianna Margulies Plastic Surgery. This photo Before After Facelift With Botox Injections Of Julianna Margulies Plastic Surgery published under Plastic Surgery category, tagged , author by admin, find webpage Before After Facelift With Botox Injections Of Julianna Margulies Plastic Surgery, you may visit website.

Date: 2014-09-30T15:49:49Z
Himachal

Bhubonpothik posted a photo:

Himachal

Date: 2014-09-30T15:49:46Z
Para terminar el mes patrio. Viva México! Desde un buque de guerra de la Armada de #México en Salina Cruz, #Oaxaca.

Tledavid posted a photo:

Para terminar el mes patrio. Viva México!  Desde un buque de guerra de la Armada de #México en Salina Cruz, #Oaxaca.

Date: 2014-09-30T15:49:48Z

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

BB code available :

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