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

Mass rallies over Greek bailout vote
Tens of thousands of people attend rival rallies in Greece ahead of Sunday's crucial referendum on an international financial bailout.
Date: Sat, 04 Jul 2015 00:44:12 GMT
Iran nuclear deal 'never closer'
Iran's foreign minister uses a YouTube posting to say a comprehensive agreement over its nuclear programme has never been closer.
Date: Sat, 04 Jul 2015 01:21:15 GMT
Exam focus 'harms' pupils' health
Schools are on the verge of becoming exam factories that damage pupils' mental health and self-esteem, says the NUT.
Date: Sat, 04 Jul 2015 00:51:46 GMT
CBI says UK economic growth slows
Surveys for the Confederation of British Industry find that manufacturing, retails and services growth slowed in the second quarter.
Date: Fri, 03 Jul 2015 23:03:35 GMT
IS claims rocket attack on Israel
Sinai Province, a group affiliated with Islamic State, says it fired three rockets into Israel from Egypt's Sinai peninsula.
Date: Sat, 04 Jul 2015 01:22:41 GMT
Brazil unveils Rio Olympics torch
Brazilian President Dilma Rousseff unveils the torch to be used at the 2016 Olympics and says the Rio Games will bring "honour and pride".
Date: Sat, 04 Jul 2015 01:04:12 GMT
Colombia has first legal euthanasia
A Colombian man with terminal cancer becomes the first person in the country to die legally by euthanasia, in a move opposed by the Church.
Date: Fri, 03 Jul 2015 20:31:26 GMT
Police response 'slow' - Tunisia PM
The slow response of Tunisia's police to last week's deadly attack in Sousse was a major problem, the country's prime minister tells the BBC.
Date: Sat, 04 Jul 2015 00:52:56 GMT
Clifford faces indecent assault charge
Former publicist Max Clifford is charged with an indecent assault allegedly committed in 1981, the Crown Prosecution Service says.
Date: Fri, 03 Jul 2015 15:05:10 GMT
Police marksman cleared of murder
A police marksman is cleared of murdering suspected armed robber Azelle Rodney, who he shot in 2005 in an operation to foil an attempted raid.
Date: Fri, 03 Jul 2015 15:41:32 GMT

cnn

Director insists N. Korean space program isn't for war
'Ordinary' citizens defend N. Korea | Drought expected to cause famine | New airport awaits more flights
Date: Fri, 03 Jul 2015 20:16:46 EDT
Terror threat over July 4 holiday
U.S. leaders at home and abroad are taking steps to increase security and surveillance ahead of the July 4 holiday, following a warning from security officials that such gatherings could be targets of a terror threat.
Date: Fri, 03 Jul 2015 18:13:05 EDT
Escapee wrote letter to daughter
Photos: How the manhunt ended | Timeline | Opinion: Why escapee is talking to cops
Date: Fri, 03 Jul 2015 15:38:55 EDT
Sex, drugs, prison corruption
CNN's Randi Kaye brings to light the corruption in a Maryland prison. According to the government, one inmate fathered five children with four different officers while in prison.
Date: Fri, 03 Jul 2015 20:16:31 EDT
GOP finds Trump 'not helpful'
Republicans seeking to broaden the party's appeal to Latino voters have a challenge: Donald Trump.
Date: Fri, 03 Jul 2015 18:08:31 EDT
'Star Trek' actor regrets 'blackface' remark
George Takei likely went where no critic of a Supreme Court justice has gone before.
Date: Fri, 03 Jul 2015 18:56:43 EDT
The most American thing to do
On July 4th, celebrate jury service, a supremely democratic act available to all--one that weds history, patriotism, self-sacrifice, dissent and community
Date: Fri, 03 Jul 2015 18:54:42 EDT
Assange: My life is in danger
France has rejected a request from Julian Assange for "protection" after the controversial WikiLeaks founder published an open letter in national newspaper Le Monde on Friday.
Date: Fri, 03 Jul 2015 13:51:57 EDT
It's fun being Obama again
Why Americans like him again | His legacy-setting week | A visual diary of his presidency
Date: Fri, 03 Jul 2015 20:19:38 EDT
Man busted in mall with AR-15
Police arrested a soldier carrying an assault rifle and ammunition at a Fayetteville, North Carolina, mall after officers received multiple reports of an armed man walking through a Macy's department store.
Date: Fri, 03 Jul 2015 16:16:44 EDT

flickr

(Untitled)

antunes.stephanie posted a photo:

Date: 2015-07-04T01:36:34Z
#3.30am #vilnius

VilniusOnline posted a photo:

#3.30am #vilnius

By maria_mai /
Source: instagram.com/p/4sdAdeNigH/ /
Published: July 04, 2015 at 03:45AM

Date: 2015-07-04T01:36:32Z
20150512國語課大富翁

flps0028 posted a photo:

20150512國語課大富翁

Date: 2015-07-04T01:36:34Z
A cool photo from Atlanta

mattmavis posted a photo:

A cool photo from Atlanta

by theonlynaomii ift.tt/1NEUzhC

Date: 2015-07-04T01:36:34Z
tmpzfBEqH

roverstays posted a photo:

tmpzfBEqH

Date: 2015-07-04T01:36:37Z
IMG_5092.jpg

Silvestro Fimiani posted a photo:

IMG_5092.jpg

Date: 2015-07-04T01:36:37Z
オープニングセッション、だいぶ入ってます!雨の中、来場ありがとうございます~!m(__)m #html5jk

moonglows76 posted a photo:

オープニングセッション、だいぶ入ってます!雨の中、来場ありがとうございます~!m(__)m #html5jk

Date: 2015-07-04T01:36:35Z
Crater Lake

Kerry Wixted posted a photo:

Crater Lake

Date: 2015-07-04T01:36:33Z
This little girl, Bella, is so cute and so kind.... #mansbestfriend #dog #daschund #bella #loveable #merionvillage

horacionieto81 posted a photo:

This little girl, Bella, is so cute and so kind....  #mansbestfriend #dog #daschund #bella #loveable #merionvillage

Date: 2015-07-04T01:36:36Z
#candycrush #candycrushsaga #needmorelevels 1025

dnyc20 posted a photo:

#candycrush #candycrushsaga #needmorelevels 1025

Date: 2015-07-04T01:36:37Z

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

BB code available :

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