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

Many feared dead in Libya sinkings
Hundreds of people are feared dead after two boats carrying up to 500 migrants capsize off the Libyan city of Zuwara.
Date: Fri, 28 Aug 2015 00:57:37 GMT
UK jihadist 'killed in drone strike'
A British man described as a "top cyber jihadist" has been killed in a military drone strike in Syria, US officials say.
Date: Thu, 27 Aug 2015 18:06:13 GMT
Facebook hits billion users in a day
Facebook boast a new record, with more than a billion people using the social network on a single day this week.
Date: Thu, 27 Aug 2015 23:47:46 GMT
Victim's father urges US gun reform
The father of Alison Parker, one of two journalists killed live on air, appeals to US President Barack Obama to push through tougher gun laws.
Date: Thu, 27 Aug 2015 22:58:36 GMT
Austria police examine lorry bodies
Police in Austria work through the night to establish how many people died in a lorry found abandoned on a motorway near the Hungarian border.
Date: Thu, 27 Aug 2015 22:09:25 GMT
Brain cells 'burn out' in Parkinson's
Brain cells in Parkinson's disease exhaust themselves and die prematurely, burning out like an "overheating motor", an early study suggests.
Date: Thu, 27 Aug 2015 23:08:59 GMT
Obama: New Orleans 'moving forward'
US president Barack Obama is set to visit New Orleans, saying the city is "moving forward" 10 years after the devastation of Hurricane Katrina.
Date: Thu, 27 Aug 2015 22:00:21 GMT
Tap water boiling 'to end soon'
Hundreds of thousands of people in Lancashire should no longer need to boil their tap water to make it safe within the coming week, United Utilities says.
Date: Fri, 28 Aug 2015 00:12:18 GMT
Post Office expands Sunday openings
The number of Post Offices opening on a Sunday will hit 3,000 this week-end, thanks to new facilities at a garden centre in Norfolk.
Date: Thu, 27 Aug 2015 23:14:17 GMT
Young drivers 'most likely' to use mobiles
Young motorists are the most likely to speak on the phone while driving, a survey for road safety charity Brake suggests.
Date: Fri, 28 Aug 2015 00:39:33 GMT

cnn

See the world's most elegant cars
Just over the hill from Monterey is Carmel-by-the-Sea, a small town on the central coast of California known for its rugged natural scenery, white sand beaches, artistic culture and relaxed ambiance.








Date: Fri, 28 Aug 2015 01:44:01 GMT
New Orleans: Tougher, feistier after Katrina
As the 10-year anniversary of Hurricane Katrina approaches, CNN Opinion asked residents and those involved in New Orleans' recovery for their take on the lessons learned, and what the city means to them. All views are the authors' own.








Date: Fri, 28 Aug 2015 01:43:27 GMT
Her Selma photo shocked a nation
Amelia Boynton Robinson, a matriarch of the civil rights movement immortalized in a photograph taken on Bloody Sunday, has died.








Date: Fri, 28 Aug 2015 01:39:34 GMT
Trump tousler: Tresses are true
It's not every day you get to meet a presidential candidate, let alone get invited up on stage to touch their hair.








Date: Fri, 28 Aug 2015 01:21:12 GMT
Ex-prep student at rape trial: I lied
A New Hampshire jury will resume deliberations Friday in the trial of Owen Labrie, a 19-year-old graduate of a prestigious prep school who's accused of raping a girl when she was 15.








Date: Fri, 28 Aug 2015 01:15:08 GMT
Obama cites inequities 'brewing for decades'
President Barack Obama returns to an outwardly thriving New Orleans Thursday to mark strides 10 years after Hurricane Katrina. But underneath the visible recovery lie persistent racial and economic inequities that haven't receded a decade since the storm.








Date: Fri, 28 Aug 2015 00:34:55 GMT
Calls from the edge: 'I ain't dead yet'









Date: Fri, 28 Aug 2015 00:33:37 GMT
Red flags in killer's firing at WDBJ?









Date: Fri, 28 Aug 2015 00:33:05 GMT
How one PTA is killing bake sales
With three children, Dee Heinz's family has participated in just about every bake sale and class fundraiser known to the modern PTA.








Date: Fri, 28 Aug 2015 00:23:29 GMT
All aboard: Life on a cruise ship
A man stands ashore and supervises two crew members working on a cruise ship's hull. Passengers get ready to board a ship after it has docked. A waiter serves a woman a teacup in one of the restaurants on the ship.








Date: Fri, 28 Aug 2015 00:18:37 GMT

flickr

260806.jpg

charliebrowncafe posted a photo:

260806.jpg

Date: 2015-08-28T02:08:51Z
DSC_0251_2

lukasz.jurgas posted a photo:

DSC_0251_2

Date: 2015-08-28T02:08:52Z
IMG_1181

jeepskinc posted a photo:

IMG_1181

Date: 2015-08-28T02:08:47Z

madmanrich posted a photo:

Date: 2015-08-28T02:08:48Z
General Winfeild Scott Handcock

jhall76288 posted a photo:

General Winfeild Scott Handcock

Date: 2015-08-28T02:08:50Z
What a guy!

dmoneyk posted a photo:

What a guy!

Date: 2015-08-28T02:08:47Z
ATV TOUR 8/27/2015

taylorb745 posted a photo:

ATV TOUR 8/27/2015

Date: 2015-08-28T02:08:48Z
8/27/15

melissalane917 posted a photo:

8/27/15

Date: 2015-08-28T02:08:50Z
Daman Tour

luhar_sunil posted a photo:

Daman Tour

Date: 2015-08-28T02:08:51Z
@guilherme__sales #Follow #Him #Boy #Hot #itsToddS #ToddJSaporito

#toddjsaporito posted a photo:

@guilherme__sales #Follow #Him #Boy #Hot #itsToddS #ToddJSaporito

boyshott2.0 ift.tt/1MYz6mk

Date: 2015-08-28T02:08:51Z

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