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

UK brands Assange ruling 'ridiculous'
The UK foreign secretary brands as "ridiculous" a UN panel's ruling that Julian Assange be allowed to go free, but the Wikileaks founder demands the decision be respected.
Date: Fri, 05 Feb 2016 17:54:11 GMT
Man shot dead at Dublin boxing weigh-in
A man is shot dead at a Dublin boxing weigh-in, in a suspected gangland attack by gunmen dressed as police officers and armed with AK47s.
Date: Fri, 05 Feb 2016 20:59:59 GMT
Massive crane collapses in New York
A large construction crane has collapsed in lower Manhattan in New York City, killing one person and injuring at least two others.
Date: Fri, 05 Feb 2016 16:25:45 GMT
Twitter shuts 125,000 'terror' accounts
Twitter has suspended more than 125,000 accounts since mid-2015 "for threatening or promoting terrorist acts", the social media company says.
Date: Fri, 05 Feb 2016 21:28:15 GMT
Refugees mass at Turkey-Syria border
Some 15,000 Syrians are massed at the Turkish border after fleeing a government offensive in Aleppo, the UN and Turkey say.
Date: Fri, 05 Feb 2016 20:43:10 GMT
Muslim women 'stopped in Labour bids'
A Muslim women's group writes to Labour leader Jeremy Corbyn claiming some women have been stopped from becoming councillors by Muslim men in their local party.
Date: Fri, 05 Feb 2016 21:05:04 GMT
Strong earthquake hits south Taiwan
A strong earthquake hits southern Taiwan with reports of collapsed buildings and no word yet on any casualties.
Date: Fri, 05 Feb 2016 21:50:49 GMT
Blinded PC family lose negligence case
The family of PC David Rathband, who was shot by Raoul Moat, lose a High Court negligence claim against Northumbria Police.
Date: Fri, 05 Feb 2016 13:06:12 GMT
Zika 'linked to nerve disorder deaths'
Colombia says three people have died after contracting the Zika virus and developing a rare nerve disorder.
Date: Fri, 05 Feb 2016 19:56:09 GMT
Cameron gets Danish backing for EU deal
David Cameron receives the backing of Denmark for his EU renegotiation after Poland said plans to limit benefits for migrants need "further discussion".
Date: Fri, 05 Feb 2016 16:49:05 GMT

cnn

Boy sentenced for killing girl over puppy
An 11-year-old boy in Jefferson County, Tennessee, has been sentenced to spend the next eight years in juvenile prison for the murder of an 8-year-old girl, according to a court document.









Date: Fri, 05 Feb 2016 21:22:49 GMT
Kerry: Russian bombs killing 'large numbers' of women, children
Secretary of State John Kerry on Friday accused Russia of bombing women and children "in large numbers" in Syria.









Date: Fri, 05 Feb 2016 21:22:30 GMT
Louisville bans itself from NCAA tourney
Graduate transfers Damion Lee and Trey Lewis headed to Louisville in hopes of getting to take part in March Madness for the first time.









Date: Fri, 05 Feb 2016 21:19:47 GMT
Officials: Suspect sucked from plane
A laptop computer may have concealed the explosives that ripped a hole in a Somali airliner Tuesday, according to a source familiar with the investigation and a Western official based in Mogadishu.









Date: Fri, 05 Feb 2016 21:12:09 GMT
Candidate is big player in fundraising system he decries










Date: Fri, 05 Feb 2016 20:58:23 GMT
Doctor sentenced in OD deaths
A Southern California doctor convicted of murder in connection with the overdose deaths of three of her patients was sentenced to 30 years to life in prison Friday by a judge in Los Angeles.









Date: Fri, 05 Feb 2016 20:49:47 GMT
Zika: Why not just kill all mosquitoes?
The mosquito was never going to win any popularity contests, but as the "deadliest animal in the world" spreads the Zika virus, it's become the target of tough talk and powerful pesticides.









Date: Fri, 05 Feb 2016 19:28:52 GMT
Bush camp puts Santorum in ad
Jeb Bush's campaign is pouncing Marco Rubio's resume — or lack thereof — in a new, edgy TV ad that features Rick Santorum's now-famous interview and draws a contrast to Bush's record as former Florida governor.









Date: Fri, 05 Feb 2016 18:56:51 GMT
So much for Trump's new strategy
So much for a new strategy.









Date: Fri, 05 Feb 2016 18:54:35 GMT
Becoming a Budweiser Clydesdale
It's baby boom season in Boonville, Missouri. As Super Bowl excitement grows across the country, John Soto keeps his cell phone handy, and his shoes near the door. The call can come any time. When it does, the next American icon will be born within minutes.









Date: Fri, 05 Feb 2016 18:50:16 GMT

flickr

Vi-Tóia na Guerra. ▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃ #paparazzi #paparazziwebradio #paparazzidivas #paparazzieventos #radio #radiopaparazzi #musa #musica #webradio #520

Marcelo Moraes posted a photo:

Vi-Tóia na Guerra. ▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃ #paparazzi #paparazziwebradio #paparazzidivas #paparazzieventos #radio #radiopaparazzi #musa #musica #webradio #520

Date: 2016-02-05T21:54:21Z
Фото из разряда #чтобыяносилабудьуменямиллион ну а пока его нет, я продолжаю верить что все мысли материализуются. И если я покажу миру что хочу ( начнём с малого , потом и люкс подтянется 😂) то непременно это будет #имиджконсультация #ИМИДЖ #СТИЛЬ #С

instalushta posted a photo:

Фото из разряда #чтобыяносилабудьуменямиллион ну а пока его нет, я продолжаю верить что все мысли материализуются. И если я покажу миру что хочу ( начнём с малого , потом и люкс подтянется 😂) то непременно это будет #имиджконсультация #ИМИДЖ #СТИЛЬ #С

by crimea_stylist ift.tt/1nSIuhF

Date: 2016-02-05T21:54:23Z
The best place Capoverde shark bay beach #kite #kites #kitebech #kitecamp #kitesurf #kitesurfen #kitesurfers #kitesurfing #kitesurfingworld #kiteboard #kiteboards #kiteboarder #kiteboarders #kiteboarding #kiteboardingzone #sharkbay #capoverde #sal #iphone

antoniogaudini posted a photo:

The best place Capoverde shark bay beach #kite #kites #kitebech #kitecamp #kitesurf #kitesurfen #kitesurfers #kitesurfing #kitesurfingworld #kiteboard #kiteboards #kiteboarder #kiteboarders #kiteboarding #kiteboardingzone #sharkbay #capoverde #sal #iphone

Date: 2016-02-05T21:54:23Z
Leeds

kf_leeds posted a photo:

Leeds

Date: 2016-02-05T21:54:20Z
"Sobretudo, amem-se sinceramente uns aos outros, porque o amor perdoa muitíssimos pecados." 1 Pedro 4:8 Imagem: @_cajadada

JoOhRamos posted a photo:

"Sobretudo, amem-se sinceramente uns aos outros, porque o amor perdoa muitíssimos pecados." 1 Pedro 4:8 Imagem: @_cajadada

Date: 2016-02-05T21:54:21Z
Photo - 027

cbmjm1 posted a photo:

Photo - 027

Date: 2016-02-05T21:54:24Z
DSC_0005

Leo_González posted a photo:

DSC_0005

Date: 2016-02-05T21:54:18Z
IMG_8821

StonewallTeulonTribune posted a photo:

IMG_8821

Date: 2016-02-05T21:54:18Z
Poller Wiesen

larsdechau posted a photo:

Poller Wiesen

Date: 2016-02-05T21:54:21Z
DSC05908.JPG

drownedvalley posted a photo:

DSC05908.JPG

Date: 2016-02-05T21:54:22Z

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

BB code available :

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