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

WHO seeks to allay Zika Olympics fears
The World Health Organization plays down concerns that the Rio Olympics will spread the Zika virus, amid calls for the Games to be postponed.
Date: Sat, 28 May 2016 17:14:55 GMT
Several hurt by lightning in W Europe
More than 40 people, most of them children, have been injured by lightning strikes, at a park in Paris and a football match in Germany.
Date: Sat, 28 May 2016 17:50:57 GMT
Rider dies in pre-TT race practice
A rider dies and another is seriously injured in a crash during the pre-TT races.
Date: Sat, 28 May 2016 12:31:03 GMT
Miliband 'call to arms' for young voters
Former Labour leader Ed Miliband issues a "call to arms", urging young people to register to vote in next month's EU referendum.
Date: Sat, 28 May 2016 17:40:37 GMT
Children 'denied mental health support'
Mental health services turned away more than a quarter of children referred for support without help in England in 2015, a report says.
Date: Sat, 28 May 2016 12:54:21 GMT
Saracens 28-20 Exeter
European champions Saracens beat Exeter Chiefs to win the Premiership final at Twickenham and complete the double.
Date: Sat, 28 May 2016 15:53:05 GMT
Officer dismissed for 'ignoring' stabbing
A Metropolitan Police sergeant is dismissed for failing to respond to a fatal attack in which a man was stabbed more than 200 times.
Date: Sat, 28 May 2016 11:58:15 GMT
Thailand speedboat British victim named
A British woman who died after a speedboat capsized near the island of Koh Samui in Thailand is named as Monica O'Connor.
Date: Sat, 28 May 2016 13:51:25 GMT
Putin visits all-male Orthodox enclave
Russian President Vladimir Putin joins celebrations to mark 1,000 years of Russian monks at the monastic enclave of Mount Athos in Greece.
Date: Sat, 28 May 2016 16:55:50 GMT
Ticking security tag sparks bomb scare
A city centre department store is evacuated after a faulty electronic security tag making a loud ticking sound was mistaken for a bomb.
Date: Sat, 28 May 2016 16:12:08 GMT

cnn

How two white men befriended the Navy's first black pilot
Jesse Leroy Brown battled poverty and racism to become the U.S. Navy's first black pilot. Faced with death over the skies of North Korea, he became something more -- a symbol of how, in times of war, color and class mean little.

Date: Sat, 28 May 2016 11:38:33 EDT
Daughter reads pilot's last love letter
Jesse Brown's daughter reads her father's last love letter to her mother before he was shot down and killed during the Korean War.
Date: Sat, 28 May 2016 11:30:26 EDT
Opinion: Women in military finally getting respect
This Memorial Day is the first in which the ban on women in ground combat is history. Last month, West Point celebrated 40 years of women in its ranks. And the first women to graduate from Army Ranger School last year have now become part of the new old guard.

Sponsored:
Drivers With No Tickets In 3 Years Read This
Do NOT pay your next car insurance bill until you try this.

Date: Sat, 28 May 2016 11:48:40 EDT
Sanders to DNC: Remove Clinton backers from platform committees
Bernie Sanders' campaign wants the Democratic National Committee to remove two Hillary Clinton supporters from key convention roles, accusing them of being "aggressive attack surrogates."
Date: Sat, 28 May 2016 12:22:47 EDT
Trump: I will solve CA water crisis
Donald Trump told voters in Fresno, California that he has a solution to the state's water crisis because there is "plenty of water."

Date: Sat, 28 May 2016 12:30:29 EDT
First on CNN: Trump eyeing big stadium for acceptance speech
Donald Trump's campaign is considering booking one of Cleveland's big sports venues for his acceptance speech in July, two GOP sources familiar with the planning of the upcoming GOP convention say.
Date: Sat, 28 May 2016 13:22:22 EDT
Lightning injures 11 in Paris park
Lightning struck a group of children and adults Saturday in a Paris park, leaving four people -- three of them children -- with life-threatening injuries, two others in critical condition and five in serious condition, authorities there said.

Popular: Radical Islam in America | Graham Opposes Republican Leader | Guns in America

Date: Sat, 28 May 2016 13:21:40 EDT
Beloved college football coach dies after battle with Parkinson's
After a decade of battling Parkinson's disease, beloved college football coach Don Horton died Saturday morning according to his wife.
Date: Sat, 28 May 2016 13:19:42 EDT
Set on fire 4, he's learned to thrive
Nearly a decade ago, 4-year-old Youssif sullenly pushed grains of rice through his lips in the kitchen of our Baghdad bureau.

Date: Sat, 28 May 2016 13:19:09 EDT
Pakistan: Light wife beating urged
The leader of a Pakistani Islamic council has proposed a bill that allows husbands to "lightly beat" their wives as a form of discipline.
Date: Sat, 28 May 2016 08:38:48 EDT

flickr

Photo

Parques @ Reunidos posted a photo:

Photo

by aslollar ift.tt/1WsE3J2

Date: 2016-05-28T18:00:25Z
2016-05-28 at 18:00 Recordings

vbradio posted a photo:

2016-05-28 at 18:00 Recordings

18:00:01 up 3 days, 19:00, 0 users, load average: 0.78, 0.67, 0.67 | temp=48.2'C | Start
18:00:13 up 3 days, 19:00, 0 users, load average: 1.43, 0.81, 0.72 | temp=48.7'C | SID plot Finished

Date: 2016-05-28T18:00:29Z
Grossglockner Hochalpenstrasse, Austria

Grodzio posted a photo:

Grossglockner Hochalpenstrasse, Austria

Date: 2016-05-28T18:00:31Z
00606E91EAE6(countryman2) motion alarm at 20160528100001

CountrymanCabin posted a photo:

00606E91EAE6(countryman2) motion alarm at 20160528100001

Date: 2016-05-28T18:00:31Z
(Untitled)

joel.maugard posted a photo:

Capturé à l'aide d'un Sony DSC-HX60.

Date: 2016-05-28T18:00:27Z
Day 8. About to get staples out.

obiwantobi posted a photo:

Day 8. About to get staples out.

Date: 2016-05-28T18:00:28Z

ryanfieldingbusphotos posted a photo:

Date: 2016-05-28T18:00:29Z
Photo0029

Gago Archangelo posted a photo:

Photo0029

Date: 2016-05-28T18:00:31Z

drewknapp1983 posted a photo:

Date: 2016-05-28T18:00:24Z
Construction site - Godwins Hollow

Godwins in Kuwait posted a photo:

Construction site - Godwins Hollow

Date: 2016-05-28T18:00:25Z

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

BB code available :

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