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

Sir Gerald had been an MP since 1970 and became the oldest serving member of the Commons in 2015.
Date: Mon, 27 Feb 2017 00:48:02 GMT
The Public Accounts Committee criticises Theresa May and the NHS for "bickering" over funding.
Date: Mon, 27 Feb 2017 00:13:27 GMT
A key Theresa May aide is criticised over his comments defending changes to the PIPs system.
Date: Sun, 26 Feb 2017 16:27:25 GMT
Consumers are now more likely to seek out better deals, but the regulator warns many still lose out.
Date: Mon, 27 Feb 2017 00:05:06 GMT
Irene Clennel has been sent back to Singapore, leaving behind her husband and sons in Britain.
Date: Mon, 27 Feb 2017 01:19:05 GMT
Sixth forms and further education colleges have been "continually squeezed" for 25 years.
Date: Mon, 27 Feb 2017 01:49:59 GMT
Elli Norkett, who has died aged 20, was the youngest player at the 2014 Rugby World Cup.
Date: Sun, 26 Feb 2017 17:47:40 GMT
The mystery surrounding the killing of Kim Jong-nam leaves more questions than answers two weeks on. Rupert Wingfield-Hayes went to Kuala Lumpur to try and find out.
Date: Sun, 26 Feb 2017 21:24:56 GMT
Labour calls for "immediate" relief for companies facing big increases after April's revaluation.
Date: Mon, 27 Feb 2017 00:32:03 GMT
As the inquiry into child sexual abuse begins its first public hearings, one victim tells his story.
Date: Mon, 27 Feb 2017 01:20:24 GMT

cnn

President Trump's nominee for secretary of the Navy, Philip Bilden, has withdrawn his name from consideration, Bilden and Secretary of Defense Jim Mattis said Sunday in statements.
Date: Mon, 27 Feb 2017 01:17:03 GMT
Will Hollywood give all the awards to a film about itself or celebrate diversity? Are we in for a night of political speeches slamming President Donald Trump? And will host Jimmy Kimmel kill or be killed?
Date: Mon, 27 Feb 2017 01:26:37 GMT
White House press secretary Sean Spicer recently checked his aides' cell phones to ensure they weren't communicating with reporters as part of an aggressive effort to stem the recent tide of White House leaks.
Date: Sun, 26 Feb 2017 22:45:55 GMT
President Donald Trump will call for a substantial increase in military spending and look to safeguard Social Security and Medicare from any cuts in his first major step toward compiling a budget proposal for the coming fiscal year, a senior administration official told CNN on Sunday.
Date: Mon, 27 Feb 2017 01:19:54 GMT
See photos from the red carpet at the 2017 Academy Awards.
Date: Mon, 27 Feb 2017 01:45:33 GMT
So you've been invited to watch the Academy Awards with friends and you haven't seen a movie in the theater since the last "Star Wars." You know, the one from almost 15 months ago.
Date: Sat, 25 Feb 2017 01:06:19 GMT
From major winners to big surprises, CNN's Stephanie Elam previews the moments that will get everyone talking about the 2017 Oscars.
Date: Sun, 26 Feb 2017 03:00:09 GMT
A little blue ribbon has become the hottest accessory for many stars on the red carpet at the 89th Annual Academy Awards. Celebrities are donning the ribbon in support of the American Civil Liberties Union (ACLU), an organization that challenged President Donald Trump's controversial travel ban in court.
Date: Mon, 27 Feb 2017 01:48:07 GMT
The Academy Awards can be perplexing, unless you're especially well versed in the difference between "sound editing" and "sound mixing." But that doesn't mean you can't have a competitive advantage with your Oscar ballot, even if you don't have money riding on it.
Date: Fri, 24 Feb 2017 20:41:34 GMT
President Donald Trump announced on Twitter on Saturday that he will not attend this year's White House correspondents' dinner.
Date: Sun, 26 Feb 2017 22:15:42 GMT

flickr

New post on i-wildest-dreams

akid63 posted a photo:

New post on i-wildest-dreams

New post on i-wildest-dreams ift.tt/2lL3SWh

Date: 2017-02-27T01:54:47Z
IP Camera E-mail Alert-md snap(2017-02-27 10:54:30)

hasegawahg posted a photo:

IP Camera E-mail Alert-md snap(2017-02-27 10:54:30)

This is a IP Camera e-mail alert.

Date: 2017-02-27T01:54:50Z
Phone photo upload

zoe krylova posted a photo:

Phone photo upload

Date: 2017-02-27T01:54:55Z

ggrobbel posted a photo:

Date: 2017-02-27T01:54:47Z
IMG_8906

Andy E. Nystrom posted a photo:

IMG_8906

Date: 2017-02-27T01:54:55Z
Lonely House

影君子 posted a photo:

Lonely House

Date: 2017-02-27T01:54:46Z
Tormented - book cover now available in full book wrap and ebook options. #skull #apocalypse #bookcovers #indiebooks #custombookcover #custombook #ebooks #ebookcoverdesign #ebookcover #graphicdesigner #ilovebooks #bookcoversforsale #bookstagram #writers #

DropDeadDesigns posted a photo:

Tormented - book cover now available in full book wrap and ebook options. #skull #apocalypse #bookcovers #indiebooks #custombookcover #custombook #ebooks #ebookcoverdesign #ebookcover #graphicdesigner #ilovebooks #bookcoversforsale #bookstagram #writers #

via Instagram ift.tt/2leuInP

Date: 2017-02-27T01:54:47Z
13 Best Anti-Aging Skin Care Products for 2017 -- Anti-Aging Skin Care Products...

BestWrinkleCream posted a photo:

13 Best Anti-Aging Skin Care Products for 2017 -- Anti-Aging Skin Care Products...

www.skincreamtrials.com/wp-content/uploads/2017/02/13-bes...
www.skincreamtrials.com/13-best-anti-aging-skin-care-prod...
#AntiAging, #Skin, #SkinCare

Date: 2017-02-27T01:54:53Z
20170225_183527

tomnewton4 posted a photo:

20170225_183527

Date: 2017-02-27T01:54:56Z

dresspumper2002 posted a photo:

Date: 2017-02-27T01:54:47Z

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