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

Labour's leadership says it will respect the decision of members, even if they want a new referendum.
Date: Sun, 23 Sep 2018 01:58:42 GMT
Christine Blasey Ford agrees to appear at a Senate panel over the Supreme Court nominee allegations.
Date: Sat, 22 Sep 2018 19:37:21 GMT
Anthony Joshua stops Alexander Povetkin in the seventh round at Wembley to retain his IBF, WBA and WBO heavyweight titles.
Date: Sat, 22 Sep 2018 22:35:04 GMT
Since 14-year-old Lizzie Lowe took her own life, a church congregation has been transformed.
Date: Sat, 22 Sep 2018 23:30:19 GMT
Solo yachtsman Abhilash Tomy is stranded in the Indian Ocean 3,700km off Perth in Western Australia.
Date: Sat, 22 Sep 2018 23:34:48 GMT
US cable giant wins rare blind auction process set by the UK's Takeover Panel with £30bn offer.
Date: Sat, 22 Sep 2018 21:55:37 GMT
The 19-year-old man died in hospital after the shooting in Walthamstow.
Date: Sun, 23 Sep 2018 02:03:02 GMT
Detective Chief Inspector Steve Ray has protected heads of state and government in his work as a close protection officer.
Date: Sat, 22 Sep 2018 23:45:55 GMT
Iran's supreme leader says "regional US puppets" were behind a gun assault in Ahvaz that killed 25.
Date: Sat, 22 Sep 2018 21:03:41 GMT
Investigation finds the number of scams involving elderly victims has almost doubled in three years.
Date: Sat, 22 Sep 2018 23:38:05 GMT

cnn

As the Senate Judiciary Committee staff negotiates with attorneys for Christine Blasey Ford, the woman who has accused Supreme Court nominee Brett Kavanaugh of a past sexual assault, over a potential hearing on Thursday, Republican staffers are working to interview those who may have information about the alleged incident.
Date: Sun, 23 Sep 2018 01:39:39 GMT
CNN's Randi Kaye speaks to Republican women in Florida about Christine Blasey Ford's sexual assault allegations against Supreme Court nominee Brett Kavanaugh.
Date: Fri, 21 Sep 2018 23:47:22 GMT
The Senate Judiciary Committee will likely propose holding a hearing next Wednesday where they will hear testimony from both Supreme Court nominee Brett Kavanaugh and Christine Blasey Ford, the woman accusing him of sexual assault, according to two sources with knowledge of the matter.
Date: Sat, 22 Sep 2018 23:56:42 GMT
CNN's Ana Cabrera reports on why more than half of all sexual assaults go unreported. For anyone who needs help, call the National Sexual Assault hotline: (800) 656-HOPE.
Date: Sat, 22 Sep 2018 21:59:42 GMT
A goal of drinking 100 beer kegs by the end of senior year. A party with a stripper. A student falling through ceilings at house parties. These are new details coming to light in Mark Judge's descriptions of his days as a Georgetown Prep student.
Date: Sat, 22 Sep 2018 20:30:56 GMT
Christine Blasey Ford, the professor accusing Supreme Court nominee Brett Kavanaugh of having sexually assaulted her when he was in high school, has previously told friends that the alleged encounter from more than 30 years ago has had a lasting effect on her life.
Date: Thu, 20 Sep 2018 22:36:31 GMT
A longtime friend of Christine Blasey Ford -- the woman accusing Supreme Court nominee Brett Kavanaugh of sexual assault -- tells CNN that Ford has previously said she needs "more than one exit door in her bedroom to prevent her from being trapped."
Date: Wed, 19 Sep 2018 04:49:54 GMT
Garrett Ventry, a communications adviser for the Senate Judiciary Committee's GOP majority who was leading the committee's response to allegations against Supreme Court nominee Brett Kavanaugh, has stepped down. CNN's Sarah Westwood has more.
Date: Sat, 22 Sep 2018 19:56:54 GMT
A former Trump official walked back her claim to the special counsel's office that she and former national security adviser Michael Flynn did not discuss conversations between him and Russia's ambassador about sanctions, according to a new report.
Date: Sun, 23 Sep 2018 00:34:23 GMT
The Trump administration on Saturday revealed its plan to make it harder for immigrants to come to or stay in the United States if they or their family may use certain public benefits.
Date: Sun, 23 Sep 2018 01:32:56 GMT

flickr

20180921_210539

esperanzacasa posted a photo:

20180921_210539

Date: 2018-09-23T02:45:42Z
Varias

esmeraldats posted a photo:

Varias

Date: 2018-09-23T02:45:42Z
หอยทาก

kobpan posted a photo:

หอยทาก

Date: 2018-09-23T02:45:44Z
New York Red Bulls vs Toronto FC

La Pelota Media posted a photo:

New York Red Bulls vs Toronto FC

MLS: Toronto FC at New York Red Bulls

Date: 2018-09-23T02:45:44Z
test123

SE_test posted a photo:

test123

Monitoring test upload from up-resume.flickr.com

Date: 2018-09-23T02:45:44Z
John Acorn singing the Nature Nut theme song on a ukulele at the steak pit in Patricia!!

Turniposaurus posted a photo:

John Acorn singing the Nature Nut theme song on a ukulele at the steak pit in Patricia!!

Date: 2018-09-23T02:45:47Z
Memories will never fade

naziera89@yahoo.com.my posted a photo:

Memories will never fade

Date: 2018-09-23T02:45:53Z
untitled-6273.jpg

kjbaumann posted a photo:

untitled-6273.jpg

Date: 2018-09-23T02:45:49Z
DSC02201_Photolemur_Processed.jpg

STSkidmore posted a photo:

DSC02201_Photolemur_Processed.jpg

Date: 2018-09-23T02:45:50Z
Taree-37241.jpg

appleby.david posted a photo:

Taree-37241.jpg

Date: 2018-09-23T02:45:48Z

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

BB code available :

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