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

Anti-austerity party wins Greek poll
The anti-austerity Syriza party wins the Greek general election promising to renegotiate the country's bailout in a challenge to international creditors.
Date: Mon, 26 Jan 2015 01:43:26 GMT
MPs: Ban fracking on carbon grounds
A committee of MPs has called for a moratorium on shale gas fracking on the grounds that it could derail efforts to tackle climate change.
Date: Mon, 26 Jan 2015 00:09:26 GMT
First female bishop to be consecrated
The Church of England will consecrate its first female bishop, Reverend Libby Lane, in a ceremony at York Minster later.
Date: Mon, 26 Jan 2015 01:41:07 GMT
Heartburn 'could be sign of cancer'
A health campaign is urging people not to ignore heartburn, because it could be a sign of stomach or oesophageal cancer.
Date: Mon, 26 Jan 2015 00:06:52 GMT
Hoax call made to David Cameron
Downing Street is to review security procedures after a hoax caller was put through to Prime Minister David Cameron, Number 10 says.
Date: Sun, 25 Jan 2015 22:11:02 GMT
BHS takeover approaches considered
Sir Philip Green is considering selling the department store chain BHS after receiving "several approaches".
Date: Sun, 25 Jan 2015 21:34:21 GMT
Boko Haram assaults Nigerian city
Nigerian Islamist Boko Haram fighters have attacked the strategically important north-eastern city of Maiduguri, with dozens reported dead.
Date: Sun, 25 Jan 2015 19:57:43 GMT
Siblings in care 'forced apart'
Over a third of children in care who have siblings are having to live apart from any of them, suggests research by a children's charity.
Date: Mon, 26 Jan 2015 00:10:46 GMT
Eighteen killed in Egypt protests
At least 18 people are killed in Egypt during street protests marking the fourth anniversary of the start of the 2011 uprising.
Date: Mon, 26 Jan 2015 01:06:16 GMT
Cosmetic surgery 'popularity falls'
The popularity of cosmetic surgery in the UK has plummeted in 2014, figures from the British Association of Aesthetic Plastic Surgeons show.
Date: Mon, 26 Jan 2015 00:06:08 GMT

cnn

NYC mayor: A storm like 'we've never seen before'
A monster snow storm is expected to slam into the northeast Monday and Tuesday, dumping heavy snow in Boston and New York, and packing winds gusting up to 65 mph. A blizzard warning is posted for an area from New Jersey to the Canadian border.
Date: Sun, 25 Jan 2015 21:03:51 EST
Opinion: How officials decide when to close schools
Officials are faced with making a tricky call on whether it's safe to allow normal life to go on when a storm is coming.
Date: Sun, 25 Jan 2015 22:22:47 EST
Progress made on nuclear cooperation
President Obama and Prime Minister Narendra Modi say they have moved forward on efforts to provide liability protection to U.S. firms that build civilian nuclear projects in India. | OBAMA: YEMEN UNREST WON'T HALT DRONES | OBAMA: DON'T BULLY UKRAINE
Date: Sun, 25 Jan 2015 09:39:24 EST
Criticism over Netanyahu visit to U.S. intensifies
White House Chief of Staff Denis McDonough says the United States shouldn't wade into Israeli politics -- even if the country's prime minister, Benjamin Netanyahu, plans to weigh in on American policies.
Date: Sun, 25 Jan 2015 19:45:56 EST
New demand: Release terrorist to save captive
A video posted online by a known ISIS supporter shows Japanese hostage Kenji Goto holding a photograph of what appears to be beheaded ISIS hostage Haruna Yukawa.
Date: Sun, 25 Jan 2015 12:40:04 EST
2 days, 2 failed attempts to raise AirAsia fuselage
On Sunday, a wire rope snapped as the fuselage reached the surface. The previous day, the wreckage cut through a strap connecting it to a big balloon.
Date: Sun, 25 Jan 2015 14:38:08 EST
2 die in Home Depot incident
Shoppers crowded into a Manhattan Home Depot store to prepare for an anticipated snowstorm streamed into the streets Sunday afternoon after an employee shot a store manager and then himself, according to NYPD spokesman Sgt. Brendan Ryan.
Date: Sun, 25 Jan 2015 21:38:17 EST
'My mom...really needs help'
Calise Manning is being called a hero for helping her pregnant mother and calling 911 operators in Kalamazoo, Michigan.
Date: Sun, 25 Jan 2015 19:47:36 EST
SAG Awards big winner is ...
"Birdman" won the big prize -- outstanding motion picture cast -- at the 21st Annual Screen Actors Guild Awards, but it was talk about heritage and diversity that captured the evening.
Date: Sun, 25 Jan 2015 23:11:28 EST
SAG Awards: Red carpet
Celebrities arrive in Los Angeles for the Screen Actors Guild Awards on Sunday, January 25.
Date: Sun, 25 Jan 2015 20:56:19 EST

flickr

_MG_6821

Kristoffer Augustin posted a photo:

_MG_6821

Date: 2015-01-26T04:19:47Z

Mary Claire Beavs posted a photo:

Date: 2015-01-26T04:19:48Z
006E06093040(Patio) sounddetect alarm at 20150125221918

snisidrocams posted a photo:

006E06093040(Patio) sounddetect alarm at 20150125221918

3cam01558.365home.org

Date: 2015-01-26T04:19:50Z
The Oracle of Panda speaks

ultramookie posted a photo:

The Oracle of Panda speaks

Date: 2015-01-26T04:19:42Z
Snapshot-New-Assiut-Barrage-Construction

Assiut.Ibrahimia H R.Environmental Group posted a photo:

Snapshot-New-Assiut-Barrage-Construction

Web Camera Warning

font { font-size: 12pt;}

Snapshot: Mon, 26 Jan 2015 06:20:00

Automatically sent by Le931.

Date: 2015-01-26T04:19:47Z
The answer to this question is completely debatable.... 😒

stephanieraeannboyd posted a photo:

The answer to this question is completely debatable.... 😒

Date: 2015-01-26T04:19:47Z
i-m-a-man

newbody_08 posted a photo:

i-m-a-man

i-m-a-man

Date: 2015-01-26T04:19:51Z
Australia day 2015

vashdima posted a photo:

Australia day 2015

Date: 2015-01-26T04:19:47Z
_MG_9437.jpg

Kam289 posted a photo:

_MG_9437.jpg

Date: 2015-01-26T04:19:37Z
20150125_Canon_SX500055

rlg posted a photo:

20150125_Canon_SX500055

Date: 2015-01-26T04:19:43Z

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

BB code available :

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