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

'No hiding' from abuse failures
There is "no hiding" from the failures of authorities in Oxfordshire where young girls were groomed, a retired detective tells Newsnight.
Date: Mon, 02 Mar 2015 22:30:39 GMT
Murder arrests in Becky Watts probe
Police arrest a man and woman on suspicion of murder in the investigation into the disappearance of teenager Becky Watts.
Date: Mon, 02 Mar 2015 19:05:46 GMT
Ex-Spurs midfielder Mackay dies
Former Tottenham, Derby, Hearts and Scotland midfielder Dave Mackay dies, aged 80.
Date: Tue, 03 Mar 2015 00:26:49 GMT
Parliament may be abandoned - Bercow
The Houses of Parliament could have to be "abandoned" within 20 years without extensive repair work, Commons Speaker John Bercow says.
Date: Mon, 02 Mar 2015 21:55:47 GMT
Iraq 'seizes districts from IS'
Iraqi forces say they have retaken some districts around Tikrit in their fight to recapture the strategic city from Islamic State.
Date: Mon, 02 Mar 2015 20:54:08 GMT
British plane spotters in UAE arrest
Three British men are arrested in the United Arab Emirates (UAE) over allegations of suspicious behaviour while plane spotting.
Date: Mon, 02 Mar 2015 20:59:04 GMT
Mother knew Emwazi's voice on video
The mother of Islamist militant Mohammed Emwazi says she immediately recognised his voice when she heard him on a hostage video released by Islamic State.
Date: Mon, 02 Mar 2015 19:23:39 GMT
England footballer in sex arrest
England and Sunderland footballer Adam Johnson is arrested on suspicion of sex with a 15-year-old girl, and later released on bail.
Date: Mon, 02 Mar 2015 20:53:40 GMT
Yarl's Wood suspension after C4 film
A member of staff is suspended at Yarl's Wood detention centre after a guard is secretly filmed calling inmates "caged animals".
Date: Mon, 02 Mar 2015 20:52:37 GMT
Eurostar suspended after track death
Eurostar services are returning to normal after a person was hit by a train in Kent.
Date: Mon, 02 Mar 2015 19:24:57 GMT

cnn

Video shows man on ground shot by LAPD
Police Chief Charlie Beck pointed to two pieces of evidence to support his statement that the man reached for the officer's gun. A bystander who took video drew a different conclusion. FULL STORY
Date: Mon, 02 Mar 2015 18:29:31 EST
Graphic video: LAPD killing
Officials are investigating an altercation between officers and a homeless man that ended when police say the suspect was fatally shot after attempting to get an officer's gun. CNN's Sara Sidner reports.
Date: Mon, 02 Mar 2015 17:38:05 EST
Opinion: It's not just about grabbing for the gun
Officers may be able to avoid the need for deadly force by rethinking how they approach suspects.
Date: Mon, 02 Mar 2015 18:23:46 EST
Model fears she may be implicated in killing of Putin critic
Model Anna Duritskaya was walking with Russian opposition leader Boris Nemtsov when he was shot and killed. Now she fears she will be implicated in his death, her mother tells CNN. FULL STORY
Date: Mon, 02 Mar 2015 18:32:05 EST
Tunnel mystery solved -- sort of
A mysterious tunnel that Canadian authorities discovered weeks ago is still a mystery, though police said Monday the case is closed and there was "nothing criminal" going on.
Date: Mon, 02 Mar 2015 16:43:56 EST
Apology in Tamir Rice case
Cleveland Mayor Frank Jackson apologized Monday for how the city had characterized the Tamir Rice case.
Date: Mon, 02 Mar 2015 17:39:03 EST
Woman's execution back on
Kelly Renee Gissendaner's supporters want last week's postponement of her execution made permanent.
Date: Mon, 02 Mar 2015 17:19:31 EST
ISIS frees Christians; why now?
After the shocking treatment by ISIS of its prisoners in recent months, the group's decision to release 19 Christian prisoners comes as a surprise. FULL STORY | PHOTOS: ISIS TERROR THREAT
Date: Mon, 02 Mar 2015 14:44:27 EST
Shatner defends his absence from Nimoy's funeral
William Shatner is defending himself on Twitter as his decision to skip Leonard Nimoy's funeral Sunday became front-page tabloid fodder.
Date: Mon, 02 Mar 2015 17:32:41 EST
Teen killed after Facebook fight
The fatal feud started with a quarrel on Facebook, devolved into a planned brawl at a park, and ended with a 14-year-old girl dead.
Date: Sun, 01 Mar 2015 11:14:10 EST

flickr

DSC04917-141017.jpg

C&P_Pics posted a photo:

DSC04917-141017.jpg

Date: 2015-03-03T00:29:52Z
1799251

ehornbrian posted a photo:

1799251

Date: 2015-03-03T00:29:58Z
Kurts Carstar and Cosmos Car Care

lauxgfx posted a photo:

Kurts Carstar and Cosmos Car Care

Date: 2015-03-03T00:29:58Z
DSCN1377

Pole Position Travel posted a photo:

DSCN1377

DSCN1377

Date: 2015-03-03T00:29:56Z
(Untitled)

teamgym posted a photo:

Date: 2015-03-03T00:29:58Z
Contender-4438.jpg

Lisa Ks Photos posted a photo:

Contender-4438.jpg

Date: 2015-03-03T00:29:50Z
(Untitled)

dneighbo posted a photo:

Date: 2015-03-03T00:29:55Z
Torrey Pine State Park

James Philipp posted a photo:

Torrey Pine State Park

Date: 2015-03-03T00:29:55Z
IMG_4122.jpg

prairieviewelem posted a photo:

IMG_4122.jpg

Date: 2015-03-03T00:29:55Z
Moxie agrees that the blanket is done #crochet #c2c #corner2corner #greenbay #blanket

WINNIE246 posted a photo:

Moxie agrees that the blanket is done #crochet #c2c #corner2corner #greenbay #blanket

Date: 2015-03-03T00:29:59Z

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

BB code available :

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