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

Hundreds dead in huge Nepal quake
At least 970 people have died as Nepal suffered its worst earthquake for more than 80 years, with deaths also reported in India, Tibet and Bangladesh.
Date: Sat, 25 Apr 2015 14:19:40 GMT
Queen marks Gallipoli centenary
The Queen attends UK ceremonies for the centenary of the WW1 Gallipoli landings, as services are held in Turkey, Australia and New Zealand.
Date: Sat, 25 Apr 2015 13:08:12 GMT
Clegg: No SNP 'life support' deal
Liberal Democrat leader Nick Clegg says he would not join a coalition government dependent on "life support" from the SNP after the general election.
Date: Sat, 25 Apr 2015 08:36:52 GMT
First black or Asian PM 'to be Tory'
The first black or Asian prime minister will be Conservative, David Cameron says, in a speech on opportunities for ethnic minority Britons.
Date: Sat, 25 Apr 2015 10:11:14 GMT
Ex-soldier in court over shooting
A former soldier appears in court charged with the attempted murder of a man who was shot dead by soldiers in County Armagh more than 40 years ago.
Date: Sat, 25 Apr 2015 11:59:01 GMT
Miliband warns of 'two-tier' NHS
Labour says leading English hospitals saw income from private patients rise by 58% since 2010 - but the Lib Dems accuse the party of "rank hypocrisy".
Date: Sat, 25 Apr 2015 11:03:27 GMT
Murder victim found dismembered
The murder victim whose body was found in a flat in Rhondda Cynon Taff is named as Tracey Woodford as police reveal she was dismembered.
Date: Sat, 25 Apr 2015 14:02:11 GMT
Farage reveals hospital treatment
UKIP leader Nigel Farage admits to being "in a great deal of pain" at the start of the general election campaign.
Date: Fri, 24 Apr 2015 23:03:04 GMT
Indonesia issues execution orders
At least three of 10 people on death row in Indonesia for drug smuggling are given formal notice of their imminent execution.
Date: Sat, 25 Apr 2015 12:43:10 GMT
Syria Islamists 'take northern town'
Islamist rebels have captured much of the north-western Syrian town of Jisr al-Shughur from government forces, activists say.
Date: Sat, 25 Apr 2015 10:50:41 GMT

cnn

Deadly, powerful earthquake strikes Nepal
A 7.8 magnitude earthquake struck Nepal, killing hundreds. Avalanches hit Mount Everest, and historic buildings in Kathmandu lay in rubble. FULL STORY | ARE YOU THERE?
Date: Sat, 25 Apr 2015 10:10:23 EDT
Earthquake triggers avalanches at Mount Everest
A 7.8 magnitude earthquake slams Nepal and is triggering avalanches at Mount Everest.
Date: Sat, 25 Apr 2015 10:10:40 EDT
Photos: Demolished buildings; search for survivors
A 7.8 magnitude earthquake centered less than 50 miles from Kathmandu rocked Nepal with devastating force early Saturday in Nepal's capital city, authorities said.
Date: Sat, 25 Apr 2015 10:14:20 EDT
2 rural counties, 142 HIV cases
The number of new HIV infections in a rural Indiana county has grown, according to the Centers for Disease Control and Prevention. The institute is working with state health leaders to control the "severe outbreak," which has spread among users of a prescription opioid called Opana.
Date: Sat, 25 Apr 2015 02:24:49 EDT
Jenner: I've always been confused
Former Olympian Bruce Jenner tells Diane Sawyer in an ABC News exclusive that he is a woman.
Date: Sat, 25 Apr 2015 08:01:59 EDT
No seat belt for Freddie Gray, and 'no excuses'
Freddie Gray was not buckled into a seat in the back of a police van while being transported following his arrest, police said Friday amid wide-ranging speculation over what happened to leave the Baltimore man with a broken back.
Date: Sat, 25 Apr 2015 04:45:54 EDT
Police paralyze unarmed man
CNN's Alina Machado reports on a police dashcam video that shows an officer shooting an unarmed suspect in Palm Beach, Florida.
Date: Fri, 24 Apr 2015 18:05:49 EDT
Wounded vet: Frat spit on us
The University of Florida and Emory University are investigating allegations that Zeta Beta Tau fraternity members disrespected wounded war veterans last weekend at Panama City Beach, Florida, by spitting, urinating and pouring beer on them.
Date: Sat, 25 Apr 2015 04:46:11 EDT
7 questions on 2 degrees
We're 2 degrees from a different world.
Date: Sat, 25 Apr 2015 08:05:52 EDT
Free speech activist shot dead
Her second floor café on a dusty industrial road was painted with dashes of psychedelic colors. And Sabeen Mahmud surrounded herself there with books, people, and discussions on technology, human rights and women's entrepreneurship.
Date: Sat, 25 Apr 2015 02:48:03 EDT

flickr

MK_BELLEVILLAGEBRIDGEICONNC_7094020134

andyfranklin68 posted a photo:

MK_BELLEVILLAGEBRIDGEICONNC_7094020134

Date: 2015-04-25T14:25:34Z
They Sleep. Same Place. Same Style. (Unlike Humans, they don't bother about color which we term as racism)

Khairul Hassan posted a photo:

They Sleep. Same Place. Same Style. (Unlike Humans, they don't bother about color which we term as racism)

Date: 2015-04-25T14:25:40Z
irinazair April 25, 2015 at 04:18PM

downyoudown posted a photo:

irinazair April 25, 2015 at 04:18PM

ift.tt/1GuhkEQ ūüíõ ift.tt/1GsXV45 ift.tt/1GuhmMU

Date: 2015-04-25T14:25:41Z

braschultz posted a photo:

Date: 2015-04-25T14:25:33Z
√Āllatkert 2015-04-24

szilard.juhasz posted a photo:

√Āllatkert 2015-04-24

Date: 2015-04-25T14:25:33Z
228

normafincher posted a photo:

228

Date: 2015-04-25T14:25:35Z
Barnsley Parkrun. Love this run, its one of the hardest Parkruns in the UK. Had a great run. Finally getting back to faster speeds after last years long layoff. My fastest time this year and 4 seconds away from my PB...maybe next time! #run #instarunner

cadguycad posted a photo:

Barnsley Parkrun. Love this run, its one of the hardest Parkruns in the UK.  Had a great run. Finally getting back to faster speeds after last years long layoff. My fastest time this year and 4 seconds away from my PB...maybe next time!  #run #instarunner

Date: 2015-04-25T14:25:39Z

jedimara77 posted a photo:

Date: 2015-04-25T14:25:35Z
78A5DD027D58(002lpsw) motion alarm at 20150425062508

stewartssec posted a photo:

78A5DD027D58(002lpsw) motion alarm at 20150425062508

Date: 2015-04-25T14:25:40Z
Exactly

misery722 posted a photo:

Exactly

Date: 2015-04-25T14:25:40Z

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

BB code available :

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