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

Cameron promises 'seven-day NHS'
All hospitals in England will provide "a truly seven-day NHS" by 2020 under a future Conservative government, David Cameron says.
Date: Sat, 28 Mar 2015 12:09:49 GMT
Knox and Sollecito cleared of murder
Amanda Knox and Raffaele Sollecito are acquitted by Italy's top appeals court of murdering British student Meredith Kercher in 2007 - the final ruling in the case.
Date: Sat, 28 Mar 2015 11:12:42 GMT
Alps co-pilot 'predicted notoriety'
The co-pilot of the Germanwings Airbus which crashed in the French Alps predicted "one day everyone will know my name", his ex-girlfriend says.
Date: Sat, 28 Mar 2015 08:29:16 GMT
Apple boss 'to donate $800m fortune'
Apple CEO Tim Cook says he will give away most of his $800m (£537m) fortune to good causes before he dies.
Date: Sat, 28 Mar 2015 09:48:03 GMT
Policeman charged with wife's murder
A police constable is charged with murdering his wife in Northamptonshire.
Date: Sat, 28 Mar 2015 10:58:57 GMT
Saudi diplomats evacuated from Yemen
Saudi Arabia evacuates its diplomats from Yemen as a Saudi-led coalition carries out a third night of air strikes.
Date: Sat, 28 Mar 2015 09:14:45 GMT
'Friendship message' from SNP to UK
Nicola Sturgeon is expected to send a message of "friendship and solidarity" to the rest of the UK during her speech to the SNP conference.
Date: Sat, 28 Mar 2015 01:05:02 GMT
Duo begin year-long space trip
US astronaut Scott Kelly and Russian cosmonaut Mikhail Kornienko arrive for a 12-month tour of duty on the International Space Station.
Date: Sat, 28 Mar 2015 01:53:36 GMT
Nigeria votes in presidential poll
Voters in Africa's most populous nation, Nigeria, are taking part in presidential elections against the background of militant Islamist violence.
Date: Sat, 28 Mar 2015 10:57:06 GMT
Inmate 'emailed own release order'
A convicted fraudster used an "ingenious" escape plot to trick prison wardens into letting him go free, a court has heard.
Date: Fri, 27 Mar 2015 20:27:07 GMT

cnn

Investigators probe co-pilot's life, secrets
Investigators are expected to question the Germanwings co-pilot's relatives, friends and co-workers to try to find out what happened. FULL STORY | The victims | Cockpit rules change | Complete coverage
Date: Sat, 28 Mar 2015 06:39:13 EDT
Workers at the crash site face grim, dangerous task
Working in pairs, rescue workers at the site of the crash of Flight 9525 must contend with high winds and treacherous terrain. FULL STORY | The victims | Cockpit rules change | Complete coverage
Date: Sat, 28 Mar 2015 07:32:17 EDT
Pilot: Why I take this personally
Date: Sat, 28 Mar 2015 03:34:33 EDT
Clinton lawyer: All emails erased
Hillary Clinton permanently deleted all the emails on the private server she used to do official business as secretary of state, the Republican lawmaker who subpoenaed the emails said late Friday.
Date: Sat, 28 Mar 2015 03:35:00 EDT
Amanda Knox conviction tossed
Italy's highest court has overturned the conviction of Amanda Knox and her former boyfriend in the 2007 slaying of her British roommate, the court announced Friday night. FULL STORY
Date: Sat, 28 Mar 2015 05:13:25 EDT
UN diplomat, 19 others killed in hotel siege
A siege that started with gunmen detonating a bomb and spraying bullets in a hotel in Somalia ended early Saturday with at least 20 people dead, authorities said.
Date: Sat, 28 Mar 2015 03:36:06 EDT
Saudis make gains in Yemen
In just a few weeks, good relations with neighbors have become a matter of survival for Yemen's President Abdu Rabu Mansour Hadi.
Date: Sat, 28 Mar 2015 08:00:10 EDT
Sex assault filmed on subway
A man has been charged with first degree aggravated sexual abuse after police say video shows him attacking a sleeping passenger on the subway.
Date: Fri, 27 Mar 2015 21:58:59 EDT
Hernandez fiancée: I hid box
The fiancée of former New England Patriots star Aaron Hernandez said at a hearing Friday that she was asked to conceal a box and take it out of her Massachusetts home after the murder of Odin Lloyd in 2013.
Date: Sat, 28 Mar 2015 05:25:16 EDT
Actress apologizes after claiming racial profiling
Actress Taraji P. Henson apologized Friday for claiming her son was racially profiled during a traffic stop in Southern California last year.
Date: Sat, 28 Mar 2015 06:11:32 EDT

flickr

Flagler pier

bobpinto77 posted a photo:

Flagler pier

Date: 2015-03-28T12:14:47Z
Tinin Rodríguez: “España estará en la élite del boxeo mundial” | AEBOX – Asociación española de boxeo http://aebox.es/tinin-rodriguez-espana-estara-en-la-elite-del-boxeo-mundial/ #angelmoreno #tininrodriguez #boxeo #vallecas #madrid #gimnasioboxeo #bronx

AEBOX posted a photo:

Tinin Rodríguez: “España estará en la élite del boxeo mundial” | AEBOX – Asociación española de boxeo http://aebox.es/tinin-rodriguez-espana-estara-en-la-elite-del-boxeo-mundial/ #angelmoreno #tininrodriguez #boxeo #vallecas #madrid #gimnasioboxeo #bronx

Date: 2015-03-28T12:14:51Z
Malatya

Talat Öncü posted a photo:

Malatya

İnönü heykeli ve Hükümet Binası // Peryod: 1970 Sonrası // Arkasiyazili; Renkli;

Date: 2015-03-28T12:14:51Z
20150328_094312

anhgiadinhbut posted a photo:

20150328_094312

Date: 2015-03-28T12:14:45Z
Screenshot_2015-03-28-00-37-04

sato kimu posted a photo:

Screenshot_2015-03-28-00-37-04

Date: 2015-03-28T12:14:50Z
Primavera Naranja - Patagonia Argentina Especialmente publicada para mi amiga @adriangeli15, que me invitó a participar del reto semanal de @loves_latino => #l_latino_naturaleza. . Se suben fotos de naturaleza con el Tag #l_latino_naturaleza hasta el 28

UNYKA posted a photo:

Primavera Naranja - Patagonia Argentina  Especialmente publicada para mi amiga @adriangeli15, que me invitó a participar del reto semanal de @loves_latino => #l_latino_naturaleza. .  Se suben fotos de naturaleza con el Tag #l_latino_naturaleza hasta el 28

Date: 2015-03-28T12:14:51Z
New Conference Number!!!!! Jump on call now! Good morning Men Reaching Men. Join us for our Saturday Morning Men of Power Call @ 8:00am est. Dial 857-232-0159 pin 609960. TOPIC: Men and Government - Who Gets the Power? https://www.facebook.com/groups/menr

leonbenjamin46 posted a photo:

New Conference Number!!!!! Jump on call now! Good morning Men Reaching Men. Join us for our Saturday Morning Men of Power Call @ 8:00am est. Dial 857-232-0159 pin 609960. TOPIC: Men and Government - Who Gets the Power? https://www.facebook.com/groups/menr

Date: 2015-03-28T12:14:51Z
Flickr-9716.jpg

Abel Chouinard posted a photo:

Flickr-9716.jpg

Date: 2015-03-28T12:14:46Z
(Untitled)

ashleighebreeden posted a photo:

Date: 2015-03-28T12:14:49Z
natalyashik.image.35011016320728

www.natalyashik.ru posted a photo:

natalyashik.image.35011016320728

Date: 2015-03-28T12:14:53Z

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