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

The UK economy grew by 0.6% in the three months to the end of June, as economic growth accelerated in the run-up to the vote to leave the EU.
Date: Wed, 27 Jul 2016 12:14:07 GMT
Christian, Muslim, Jewish and Buddhist representatives call for more security at places of worship after the jihadist murder of an elderly priest in his own church.
Date: Wed, 27 Jul 2016 13:20:31 GMT
Police investigating the attempted abduction of an RAF serviceman release e-fits of the men they suspect were involved.
Date: Wed, 27 Jul 2016 12:31:42 GMT
GlaxoSmithKline is to invest £275m into its UK manufacturing sites, saying the country remains "an attractive location" despite the Brexit vote.
Date: Wed, 27 Jul 2016 11:38:41 GMT
Leadership challenger Owen Smith sets out a series of policies to tackle inequality and says Labour should be "smashing the Conservatives".
Date: Wed, 27 Jul 2016 12:06:01 GMT
The Ice Bucket Challenge that went viral in 2014 has funded the discovery of an important gene in the neurodegenerative disease ALS, the ALS Association says.
Date: Wed, 27 Jul 2016 08:58:22 GMT
Police in Pakistan launch a murder investigation into the death of a woman whose husband says she was the victim of a so-called honour killing.
Date: Wed, 27 Jul 2016 12:02:53 GMT
The crashed remains from Flight MH370 could be as much as 500km further north than the current search area, argues a new modelling study.
Date: Wed, 27 Jul 2016 12:18:42 GMT
At least 40 people are killed by a truck bombing in the Kurdish-controlled city of Qamishli in north-east Syria, state TV reports.
Date: Wed, 27 Jul 2016 12:34:26 GMT
A charity trains its counsellors to help parents who fear their children are being radicalised.
Date: Wed, 27 Jul 2016 12:58:46 GMT

cnn

Hillary Clinton will shatter another glass ceiling on Tuesday when she becomes the first female presidential nominee of a major party in the nation's 240-year history.
Date: Wed, 27 Jul 2016 13:17:26 GMT
Sen. Bernie Sanders got emotional when his brother, Larry, cast the Democrats Abroad delegation's votes in favor of Bernie Sanders during the Democratic National Convention roll call.
Date: Wed, 27 Jul 2016 01:59:09 GMT
The Democratic Party gathered in Philadelphia on Tuesday for the second night of its convention, and CNN's Reality Check Team put the speakers' statements and assertions to the test.
Date: Wed, 27 Jul 2016 13:20:16 GMT
From suffragette Elizabeth Cady Stanton, the first woman to run for Congress, to Supreme Court Justice Sonia Sotomayor, take a look back at some female firsts in politics.
Date: Wed, 27 Jul 2016 10:13:29 GMT
Hillary Clinton is calling on her biggest advocate Tuesday night.
Date: Wed, 27 Jul 2016 05:57:40 GMT
Traditionally, the spouses of major-party nominees get a speaking slot at the national convention to humanize the candidate. Former presidents speak to remind the faithful of their history. Bill Clinton falls into both categories, but he had a far more difficult task in closing out the second night of the Democratic convention in Philadelphia on Tuesday night; he needed to find a way to knit the party back together again.
Date: Wed, 27 Jul 2016 11:25:14 GMT
Geneva Reed-Veal, mother of Sandra Bland, speaks about her daughter following chants of "Black Lives Matter" at the Democratic National Convention.
Date: Wed, 27 Jul 2016 02:01:24 GMT
"You wanna try me, right?"
Date: Wed, 27 Jul 2016 11:16:57 GMT
A Malawian man who bragged about having sex with underage girls to mark their passage into adulthood has been arrested, authorities say.
Date: Wed, 27 Jul 2016 11:03:53 GMT
Throwback to 2014, the year of the Ellen DeGeneres Oscar selfie, Pharrell's giant hat, and the Ice Bucket Challenge. These fads "broke the internet" (a phrase also coined in 2014) dominating social media feeds across the globe.
Date: Wed, 27 Jul 2016 12:59:44 GMT

flickr

DSCN1168

polarekpl posted a photo:

DSCN1168

Date: 2016-07-27T13:25:58Z
Ibiza Dreamer

Riccardo Dressadore concept designer posted a photo:

Ibiza Dreamer

Date: 2016-07-27T13:25:58Z

dafydd.stuttard posted a photo:

Date: 2016-07-27T13:25:59Z
10 Best Bridge Camera 2016

mewaqascheema posted a photo:

10 Best Bridge Camera 2016

Bridge cameras, as the name suggest are designed to bridge the gap between DSLRs and compact cameras. They offer a identical user experience and level of manual control as of a DSLR. They are also called superzooms, or ultra-zooms.
The Bridge camera looks like a DSLR in appearance, and a casual...

Read More : 10 Best Bridge Camera 2016

Date: 2016-07-27T13:26:01Z
untitled-102.jpg

robdsn posted a photo:

untitled-102.jpg

Date: 2016-07-27T13:25:59Z
LJ61NWG • Go-Ahead London

Sam's Bus Photos posted a photo:

LJ61NWG • Go-Ahead London

Date: 2016-07-27T13:26:01Z
Tree on an island

Vernon Burnett posted a photo:

Tree on an island

Date: 2016-07-27T13:26:02Z
Alexandra Badoi

Ciobanu Cosmin posted a photo:

Alexandra Badoi

Date: 2016-07-27T13:26:03Z
_VGL4242.jpg

VladimirLiubarsky posted a photo:

_VGL4242.jpg

Date: 2016-07-27T13:25:58Z
Proyecto 248 - Paseos -

Josetxu_GRANADA posted a photo:

Proyecto 248 - Paseos -

Dcim\100gopro\G0112434.

Date: 2016-07-27T13:26:02Z

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