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

UK 'not deporting enough criminals'
The UK's spending watchdog criticises ministers for failing to deport more foreign national offenders - saying performance has barely improved over eight years.
Date: Wed, 22 Oct 2014 05:30:30 GMT
US probes 'stray Syria air drop'
The US Pentagon says it is examining an Islamic State video appearing to show militants with a weapons bundle intended for Kurdish fighters.
Date: Wed, 22 Oct 2014 03:49:13 GMT
GPs to get £55 for dementia diagnoses
Doctors in England will be paid £55 every time they diagnose dementia, health chiefs say, but the scheme is criticised by a patients' group.
Date: Wed, 22 Oct 2014 04:35:58 GMT
Briton found with throat cut in Bali
A British man is found dead with his hands tied and throat cut on the Indonesian island of Bali, the British embassy in Jakarta says.
Date: Wed, 22 Oct 2014 06:09:09 GMT
Father of IS hostage John Cantlie dies
Paul Cantlie, the father of Islamic State hostage John Cantlie, has died, less than two weeks after making a plea for his son to be released.
Date: Tue, 21 Oct 2014 23:12:01 GMT
Ebola serum for Africa 'in weeks'
Treatments to tackle the Ebola outbreak in West Africa should become available in the coming weeks and months, says the World Health Organization.
Date: Tue, 21 Oct 2014 22:59:34 GMT
Cities need more power, says study
Allowing UK cities to make their own decisions on tax and spending could boost economic growth by £79bn a year by 2030, a year-long study concludes.
Date: Wed, 22 Oct 2014 06:10:45 GMT
Move to stop charities funding terror
The charities watchdog is to receive an extra £8m and new powers to help it tackle charities being used to fund crime and terrorism, the prime minister says.
Date: Wed, 22 Oct 2014 01:54:01 GMT
UN chief plans Gaza conflict inquiry
UN Secretary General Ban Ki-moon says he is setting up an inquiry into attacks on UN facilities during Israel's recent offensive in the Gaza Strip.
Date: Wed, 22 Oct 2014 03:54:24 GMT
US editor Ben Bradlee dies at 93
Ben Bradlee, the editor of the Washington Post during the Watergate scandal that toppled President Nixon, has died aged 93, the newspaper says.
Date: Wed, 22 Oct 2014 02:03:30 GMT

cnn

What Pistorius faces in prison
A double amputee, he needs prosthetic limbs to get around and activists warn South African prisons are tough for even the fittest inmates.
Date: Wed, 22 Oct 2014 00:21:38 EDT
The trial in 90 seconds
A look back at the trial of South Africa's most famous disabled athlete.
Date: Tue, 21 Oct 2014 05:52:32 EDT
Helmet cam shows shot to head
U.S. military footage posted to YouTube shows a Marine survive a head shot by a sniper in Afghanistan in 2013.
Date: Tue, 21 Oct 2014 06:40:21 EDT
From Jewish soccer to ISIS suspect
He was once part of Germany's largest Jewish sports club -- but now he's the first suspected member of ISIS to stand trial in a country that has been left shocked by his alleged radicalization.
Date: Tue, 21 Oct 2014 08:29:45 EDT
Teen runaway a new face for ISIS
A 17-year-old alleged jihadi from Australia vows to raise the ISIS flag over the White House and Buckingham Palace.
Date: Tue, 21 Oct 2014 16:49:42 EDT
Canada: Man strikes 2 soldiers, kills 1
Canadian authorities believe a man who allegedly struck two soldiers with his car Monday -- killing one of them -- may have been "radicalized."
Date: Tue, 21 Oct 2014 17:08:16 EDT
Report: Afghan drug war is failing
America's drug war in Afghanistan is failing badly, a U.S. government watchdog says in a new report.
Date: Tue, 21 Oct 2014 23:50:25 EDT
Watch car drive itself at 150 mph
Audi raced its self-piloted car around a track with no one inside.
Date: Tue, 21 Oct 2014 19:31:48 EDT
Brazilian man admits 39 murders
A Brazilian man nicknamed "handsome" serial killer has confessed to 39 murders. CNN's Shasta Darlington reports.
Date: Tue, 21 Oct 2014 12:01:36 EDT
Gay marriage spreads in 'red' states
It's a big week for same-sex marriages in the United States, as more "red" states begin performing them, and activists launch a new push in the Deep South.
Date: Tue, 21 Oct 2014 13:36:16 EDT

flickr

2014-10-22_02-18-23

linmengju68 posted a photo:

2014-10-22_02-18-23

Date: 2014-10-22T06:18:39Z
Viaje

Haras de la Parra posted a photo:

Viaje

Date: 2014-10-22T06:18:42Z
DSCN9386

smokkeyy posted a photo:

DSCN9386

Date: 2014-10-22T06:18:44Z
WMCFW 2014 The Leake

free admission posted a photo:

WMCFW 2014 The Leake

Date: 2014-10-22T06:18:39Z
Bonjour Une fleur nous offre sa beauté on ne peut que l'admirer La vie est un très long chemin on ne peut suivre que son destin L'amitié est un beau partage on ne peut en récolter que les avantages Vers vous cette pensée en signe d'amitié je vous souhaite

electrosat posted a photo:

Bonjour Une fleur nous offre sa beauté on ne peut que l'admirer La vie est un très long chemin on ne peut suivre que son destin L'amitié est un beau partage on ne peut en récolter que les avantages Vers vous cette pensée en signe d'amitié je vous souhaite

Date: 2014-10-22T06:18:47Z
leafy birds :) literally

▓▒░ TORLEY ░▒▓ posted a photo:

leafy birds :) literally

silent sparrow

Visit this location at The Seasons Story - Autumn 1 Year Anniversary in Second Life

Date: 2014-10-22T06:18:38Z
Hijau tu warna kesukaan nabi. Macam saya suka kan soft pink, merah, putih, baby blue, I love green very much. Until I become a pharmacist in a genaral hospital setting. I feel like vomiting whenever see green. It shows my workload pfft. Haha. #latergram

nurul shariff posted a photo:

Hijau tu warna kesukaan nabi. Macam saya suka kan soft pink, merah, putih, baby blue, I love green very much. Until I become a pharmacist in a genaral hospital setting. I feel like vomiting whenever see green. It shows my workload pfft. Haha.   #latergram

Date: 2014-10-22T06:18:42Z
Cat 4/5 - 2014 Carpenter Park Cyclocross Race

Jason L. Ward posted a photo:

Cat 4/5 - 2014 Carpenter Park Cyclocross Race

Cat 4/5 - 2014 Carpenter Park Cyclocross Race

Date: 2014-10-22T06:18:44Z
It's me Summit...Sp Saheb

summit p@ncholi1 posted a photo:

It's me Summit...Sp Saheb

Date: 2014-10-22T06:18:45Z
upload

tjtanler posted a photo:

upload

Date: 2014-10-22T06:18:41Z

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

BB code available :

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