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

The PM is stressing immigration changes in a CBI speech - amid the threat of a leadership challenge.
Date: Mon, 19 Nov 2018 11:15:45 GMT
Carlos Ghosn, a towering figure in the car industry, is accused of under-reporting his pay package.
Date: Mon, 19 Nov 2018 11:23:49 GMT
Some 3,000 doctors face scrutiny after an unqualified New Zealander practised in the UK for 22 years.
Date: Mon, 19 Nov 2018 08:04:47 GMT
Passengers face travel misery as services into both Victoria and Waterloo stations are disrupted.
Date: Mon, 19 Nov 2018 11:24:15 GMT
Emily Pratt, six, took part in the treatment trial and can now tolerate foods with traces of peanuts.
Date: Mon, 19 Nov 2018 10:10:45 GMT
For 44 years, Deke Duncan's Radio 77 has boasted "the smallest audience in the country" - his wife.
Date: Mon, 19 Nov 2018 01:31:50 GMT
The Finnish leader is not sure where Donald Trump got the idea that Finland rakes its forest floors.
Date: Mon, 19 Nov 2018 10:12:36 GMT
A single mum from Merseyside says she was forced into sex work due to delays in her benefit payment.
Date: Mon, 19 Nov 2018 11:00:22 GMT
Firms said higher running costs of small stores were behind variations such as bananas costing 9p or 25p.
Date: Mon, 19 Nov 2018 01:19:29 GMT
Volunteers made 1,000 bowls of chicken soup for east London's homeless.
Date: Mon, 19 Nov 2018 00:49:26 GMT

cnn

President Donald Trump is responding to a midterm election that represented a rebuke for his polarizing and aggressive behavior by recommitting to his brazen political style, setting up a turbulent two years to come.
Date: Mon, 19 Nov 2018 06:00:12 GMT
President Donald Trump dismissed retired Adm. William McRaven, the overseer of the operation that killed Osama bin Laden, as a "Hillary Clinton backer" and criticized the military for having not killed bin Laden sooner during a Fox News interview that aired on Sunday.
Date: Sun, 18 Nov 2018 22:29:08 GMT
Retired Adm. William McRaven, the man who oversaw the 2011 Navy SEAL raid that killed Osama bin Laden, issued a stunning rebuke of President Donald Trump's decision to revoke the security clearance of former CIA Director John Brennan, defending the former spy chief as "one of the finest public servants I have ever known."
Date: Thu, 16 Aug 2018 21:38:10 GMT
President Donald Trump said "it's going to be up to him" when asked if he would accept any move by acting Attorney General Matthew Whitaker to curtail special counsel Robert Mueller's investigation.
Date: Sun, 18 Nov 2018 23:55:14 GMT
You could write an encyclopedia about the ways in which Barack Obama is different from his successor, Donald Trump. On Saturday, Michelle Obama provided a more concise explanation for what distinguishes the two presidents from one another: Obama put country first; Trump puts himself first.
Date: Sun, 18 Nov 2018 21:16:10 GMT
• Catastrophic fire isn't even halfway finished • Evacuees take refuge in Walmart parking lot • See Paradise before and after the deadly Fire
Date: Mon, 19 Nov 2018 10:53:47 GMT
• Opinion: Trump approach is 'ignorance is bliss' • Trump: I don't need to hear killing tape
Date: Mon, 19 Nov 2018 10:12:01 GMT
Nissan said Monday that it has uncovered "significant acts of misconduct" by Chairman Carlos Ghosn.
Date: Mon, 19 Nov 2018 10:41:54 GMT
Former model and "Baywatch" star Pamela Anderson has hit out at the Australian Prime Minister for laughing off her plea to have detained Wikileaks founder Julian Assange brought back to his home country.
Date: Mon, 19 Nov 2018 07:10:11 GMT
A North Carolina schoolteacher who disappeared last month while out for a walk in Mexico is dead, according to a post on the verified Facebook page of the Governor of Chihuahua.
Date: Mon, 19 Nov 2018 00:37:42 GMT

flickr

DSC_2387.jpg

JohnMawer posted a photo:

DSC_2387.jpg

Date: 2018-11-19T11:41:45Z
Kinh tế Thái Lan tăng trưởng yếu nhất kể từ năm 2014

feed.dkn.tv posted a photo:

Kinh tế Thái Lan tăng trưởng yếu nhất kể từ năm 2014

via Đại Kỷ Nguyên - Feed - ift.tt/2Q2lZIP

Date: 2018-11-19T11:41:35Z
'Better Off Dead', segundo single de adelanto del nuevo trabajo de QUITTERS

rockypunkto posted a photo:

'Better Off Dead', segundo single de adelanto del nuevo trabajo de QUITTERS

via Noticias y actualidad de Rock y Punk - Rockypunkto ift.tt/2QZP5py

Date: 2018-11-19T11:41:36Z
006VJ5Jxgy1fxd8okiue5j32233344qs

Vya921 posted a photo:

006VJ5Jxgy1fxd8okiue5j32233344qs

Date: 2018-11-19T11:41:38Z
IMG-20180911-WA0006

akuyani94 posted a photo:

IMG-20180911-WA0006

Date: 2018-11-19T11:41:51Z
DNG36CIDv207818.tsDT2018_11_19S18@41@10C0.jpg

haivandn2018 posted a photo:

DNG36CIDv207818.tsDT2018_11_19S18@41@10C0.jpg

Date: 2018-11-19T11:41:50Z
2018-11-19_06-36-54

Informatika.com posted a photo:

2018-11-19_06-36-54

Date: 2018-11-19T11:41:36Z
BOSNA Müslüman Katliamında İSRAİL Parmağı

filistinhaberajansı posted a photo:

BOSNA Müslüman Katliamında İSRAİL Parmağı

BOSNA Müslüman Katliamında İSRAİL Parmağı filistin.info/bosna-musluman-katliam-israil/

Date: 2018-11-19T11:41:38Z
evening

kobpan posted a photo:

evening

Date: 2018-11-19T11:41:39Z
2018-11-19_12-41-22

alfrymarmugi posted a photo:

2018-11-19_12-41-22

Date: 2018-11-19T11:41:41Z

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

BB code available :

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