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

Video of British hostage released
A new video is released showing a British man, identified as journalist John Cantlie, believed to be held hostage by Islamic State militants.
Date: Thu, 18 Sep 2014 17:37:01 GMT
Millions in historic Scottish vote
Millions of people in Scotland are heading to the polls to vote on whether the country should stay in the UK or become an independent nation.
Date: Thu, 18 Sep 2014 16:38:32 GMT
Alice suspect is convicted murderer
A man wanted in connection with the disappearance of schoolgirl Alice Gross was convicted of murdering his wife in Latvia, police reveal.
Date: Thu, 18 Sep 2014 16:04:45 GMT
Women convicted of 'pyramid scheme'
Six women are convicted of running a "pyramid scheme" in south-west England and south Wales in which thousands of people lost money.
Date: Thu, 18 Sep 2014 14:31:51 GMT
No DNA matches in Thai murder probe
Thai police admit they are struggling to find the killers of two British tourists, after it emerged crime scene DNA did not match that of any suspect.
Date: Thu, 18 Sep 2014 11:14:40 GMT
Ukraine president urges US support
Ukrainian President Petro Poroshenko calls on the US to provide military assistance to his country to counter pro-Russian eastern separatists.
Date: Thu, 18 Sep 2014 16:44:05 GMT
NHS staff vote for strike action
NHS workers in England have voted in favour of striking over pay.
Date: Thu, 18 Sep 2014 13:08:43 GMT
Clash over car tax disc changes
Motorist organisation, the RAC, and the DVLA have clashed over the policing of car tax with claims of increased evasion amid changes to the system.
Date: Thu, 18 Sep 2014 09:03:18 GMT
Briton killed in Santorini incident
A British man has been killed in an incident on the Greek island of Santorini, the Foreign Office confirms.
Date: Thu, 18 Sep 2014 15:50:57 GMT
Archbishop admits 'doubts about God'
Archbishop of Canterbury Justin Welby says he sometimes has doubts in his belief in God, he tells the BBC.
Date: Thu, 18 Sep 2014 15:30:21 GMT

cnn

Missing student in surveillance videos
Surveillance videos show her walking and running alone the night she went missing, but was there foul play in the disappearance of a University of Virginia student?
Date: Wed, 17 Sep 2014 20:48:25 EDT
Calif. fire 'just out of control'
California's Governor has declared a state of emergency in two counties in the north of the state where wildfires have torched tens of thousands of acres, destroying some homes and threatening others.
Date: Thu, 18 Sep 2014 13:02:05 EDT
Rob Ford has aggressive cancer
The ailment adds to the list of struggles facing Toronto Mayor Rob Ford, whose fall from grace began with the release of a video that appeared to show him smoking crack cocaine.
Date: Thu, 18 Sep 2014 03:29:44 EDT
Report: Ferguson cop testifies
Shortly after being given more time to weigh evidence in black teenager Michael Brown's fatal shooting, a Missouri grand jury heard from the man at the center of it -- Ferguson police Officer Darren Wilson -- a local newspaper reported.
Date: Thu, 18 Sep 2014 03:29:49 EDT
Facility trains kids to shoot guns
Children in Missouri are learning about guns and gun safety. CNN affiliate KSPR reports.
Date: Thu, 18 Sep 2014 07:03:31 EDT
And the world's busiest airport is ...
Beijing, London, Tokyo, Chicago and Los Angeles have some of the busiest airports on Earth, but the world's busiest is actually ...
Date: Wed, 17 Sep 2014 15:10:40 EDT
Toothpaste leaves this in gums?
Dental hygienists have been finding mysterious tiny blue specks on people's gums and the culprit is the toothpaste.
Date: Thu, 18 Sep 2014 12:27:48 EDT
Sharon Osbourne rants about U2
Note to the Grammys: You may not want to seat Sharon Osbourne anywhere near U2 at the next awards show.
Date: Thu, 18 Sep 2014 07:41:21 EDT
'Facts of Life' stars reunite
A little good, a little bad and a whole lot of nostalgia were in play at the Paley Center's "Facts of Life" reunion in Beverly Hills, California, on Monday.
Date: Wed, 17 Sep 2014 17:22:36 EDT
30 years later, Cosby sweater still rules
The real story behind the "Cosby sweater" and why it still rules.
Date: Thu, 18 Sep 2014 08:57:26 EDT

lockergnome (hidden CDATA)

The lockergnome feed seems to be down.

lockergnome

The lockergnome feed seems to be down.

flickr

DSC_7377.jpg

Foto M@lin posted a photo:

DSC_7377.jpg

Date: 2014-09-18T17:43:11Z
Nom Nom Nom. #Giraffe #OregonZoo #Portland #Oregon #PacificNorthwest #UpperLeftUSA #Roadtrip #Travel #Wanderlust @tfe1020

bezbiletnica posted a photo:

Nom Nom Nom. #Giraffe #OregonZoo #Portland #Oregon #PacificNorthwest #UpperLeftUSA #Roadtrip #Travel #Wanderlust @tfe1020

by katieann1020 ift.tt/1wIHjmq

Date: 2014-09-18T17:43:12Z
00606E91EAE6(countryman2) motion alarm at 20140918104227

CountrymanCabin posted a photo:

00606E91EAE6(countryman2) motion alarm at 20140918104227

Date: 2014-09-18T17:43:12Z
(Untitled)

marthahandler posted a photo:

Date: 2014-09-18T17:43:11Z
DSC_0307

Perspectivas posted a photo:

DSC_0307

Date: 2014-09-18T17:43:10Z
It's so stinkin' cute when she sleeps with her mouth open. P.S. Love those lashes!

~ Janelle ~ posted a photo:

It's so stinkin' cute when she sleeps with her mouth open. P.S. Love those lashes!

Date: 2014-09-18T17:43:11Z

louispastir posted a photo:

Date: 2014-09-18T17:43:12Z
upload

emceweniv posted a photo:

upload

Date: 2014-09-18T17:43:12Z
Tag a small business! Another satisfied customer! SHODSANTIAGO@gmail

MzK.Davis posted a photo:

Tag a small business! Another satisfied customer! SHODSANTIAGO@gmail

by shodsantiago ift.tt/YVwHmd

Date: 2014-09-18T17:43:13Z
DSCF0052

marieranera posted a photo:

DSCF0052

Date: 2014-09-18T17:43:03Z

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

BB code available :

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