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

David Cameron felt "badly let down" by Theresa May during the EU referendum campaign after she repeatedly refused to back him, his former director of communications says.
Date: Sun, 25 Sep 2016 03:54:20 GMT
Cyclist Sir Bradley Wiggins tells the BBC he was not trying to gain an unfair advantage from being allowed to use a banned steroid before major races.
Date: Sun, 25 Sep 2016 08:39:58 GMT
The Duke and Duchess of Cambridge arrive in Canada with their children, five years after visiting the country as newly-weds.
Date: Sun, 25 Sep 2016 07:46:59 GMT
Junior doctors in England suspend a series of five-day strikes over the next three months, the British Medical Association announces, following concerns over patient safety,
Date: Sat, 24 Sep 2016 21:17:29 GMT
Police investigating claims that Pippa Middleton's iCloud account was hacked arrest a 35-year-old man in Northamptonshire.
Date: Sat, 24 Sep 2016 21:25:44 GMT
Police in Charlotte, North Carolina, release officer bodycam and dashcam footage of a controversial shooting of a black man last week.
Date: Sun, 25 Sep 2016 00:46:11 GMT
A suspect sought over a deadly gun attack in Washington state has been arrested and named as Arcan Cetin, 20, from Turkey.
Date: Sun, 25 Sep 2016 05:11:16 GMT
UN chief Ban Ki-moon is "appalled by the chilling military escalation" in the war-torn Syrian city of Aleppo, with the UN Security Council due to meet.
Date: Sun, 25 Sep 2016 04:31:22 GMT
A Jordanian writer charged with offending Islam after sharing a satirical cartoon on his Facebook page is killed in the capital Amman.
Date: Sun, 25 Sep 2016 08:22:36 GMT
Jeremy Corbyn promises to "wipe the slate clean" and bring Labour together after he is re-elected as leader, defeating Owen Smith.
Date: Sat, 24 Sep 2016 20:30:28 GMT

cnn

The lone suspect in the fatal shooting of five people in a Washington state mall is in custody after a nearly 24-hour manhunt, authorities said early Sunday.
Date: Sun, 25 Sep 2016 07:12:22 GMT
Authorities gather in Oak Harbor, Washington, to discuss the capture of mall shooting suspect, 20-year-old Arcan Cetin.
Date: Sun, 25 Sep 2016 05:53:30 GMT
Neither dashboard nor body camera footage shows Keith Lamont Scott pointing a gun at police officers.
Date: Sun, 25 Sep 2016 03:21:25 GMT
The family of Keith Lamont Scott has released cell phone video, recorded by his wife, that shows the moments leading to his fatal shooting by police Tuesday in Charlotte, North Carolina.
Date: Fri, 23 Sep 2016 20:52:46 GMT
Obama said Saturday that the new Smithsonian museum devoted to African-American history elevates the often-overlooked impact of black Americans.
Date: Sat, 24 Sep 2016 23:44:51 GMT
It's a tense waiting game for people facing flood threats from swollen rivers and creeks in parts of Iowa and Wisconsin.
Date: Sun, 25 Sep 2016 00:11:09 GMT
Ali was clearing debris from a room that used to be part of a hospital. The ceasefire in Aleppo was still tentatively holding, and the 12-year-old boy was taking advantage of a lull in the fighting to carve out a new place to live. Shihan, a government-held area, used to be right on the front line; now, after years of fighting, it lies in ruins. Displaced people were moving into the bombed-out ruins.
Date: Sat, 24 Sep 2016 05:08:31 GMT
Most white working-class voters who are evangelical Christians -- like most white evangelicals -- would consider voting for Donald Trump.
Date: Sat, 24 Sep 2016 22:28:04 GMT
A 17-year-old girl is missing and believed to be in danger after police found three bodies at the home where she lived in Southern California.
Date: Sun, 25 Sep 2016 07:34:53 GMT
Republicans are praising Hillary Clinton as the most skilled and experienced politician ever to stand behind a podium. Democrats note Donald Trump, a former reality star, is especially savvy on television.
Date: Sat, 24 Sep 2016 17:39:55 GMT

flickr

The Tractor Parade

FotoFling Scotland posted a photo:

The Tractor Parade

Aberfeldy Highland Games 2015

Date: 2016-09-25T08:45:25Z
105.9.25LASY積木課程主題:直升機

kelly71168@kimo.com posted a photo:

105.9.25LASY積木課程主題:直升機

Date: 2016-09-25T08:45:29Z
Go north east 3885

ryan the bus spotter posted a photo:

Go north east 3885

Date: 2016-09-25T08:45:30Z
IMG_9209.JPG

sailracer1 posted a photo:

IMG_9209.JPG

@copy; SailRacer

Date: 2016-09-25T08:45:27Z
Dog saves kitten from biting cat

wcsaw posted a photo:

Dog saves kitten from biting cat

The pet ( who is the elder-brother of the kitten from an earlier batch ) was using sleep. Then kitten began playing on their tail while the pet suddenly snatched the …
source
Sharing Is Caring

www.petssitterr.com/dog-saves-kitten-from-biting-cat/

Date: 2016-09-25T08:45:29Z
New photo added to gallery

krishnadasnair posted a photo:

New photo added to gallery

New photo added to gallery via Android ift.tt/2daCDCt

Date: 2016-09-25T08:45:30Z

stevejalland posted a photo:

Date: 2016-09-25T08:45:31Z
IMG_20160627_180819

michellewesterhuis posted a photo:

IMG_20160627_180819

Date: 2016-09-25T08:45:32Z
Anderthalb Tage an der beeindruckend schönen Ostküste Korsikas; u.a. mit riesigem Wolkenbruch

joni950 posted a photo:

Anderthalb Tage an der beeindruckend schönen Ostküste Korsikas; u.a. mit riesigem Wolkenbruch

Date: 2016-09-25T08:45:33Z

P n J posted a photo:

Date: 2016-09-25T08:45:31Z

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