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 singer is awarded £210,000 in damages after the BBC breached his privacy with its reporting of a police raid at his home.
Date: Wed, 18 Jul 2018 10:04:46 GMT
The tech entrepreneur lashed out at a British cave diver in Thailand for mocking his mini-sub.
Date: Wed, 18 Jul 2018 09:41:02 GMT
The European Commission has claimed Android unfairly extended Google's dominance of search.
Date: Wed, 18 Jul 2018 09:40:04 GMT
Dee-Ann Kentish-Rogers is the first black woman to be chosen as Miss Universe Great Britain since the competition started in 1952.
Date: Wed, 18 Jul 2018 06:59:21 GMT
The 12 boys and their football coach will make their first public appearance since being rescued.
Date: Wed, 18 Jul 2018 10:06:17 GMT
The hot weather and low reservoir levels have uncovered the ruins of a lost village in Cumbria.
Date: Tue, 17 Jul 2018 23:54:04 GMT
After Prime Minister's Questions, the PM will be grilled by senior MPs before rallying her backbenchers.
Date: Wed, 18 Jul 2018 08:04:05 GMT
This may explain the sharp fall in teen pregnancies, the British Pregnancy Advisory Service says.
Date: Tue, 17 Jul 2018 23:41:15 GMT
The Barrow and Furness MP says Labour has been "taken over by the hard left".
Date: Wed, 18 Jul 2018 09:58:32 GMT
The evidence around omega-3 supplements is flimsy at best, say experts.
Date: Wed, 18 Jul 2018 08:31:44 GMT

cnn

Vladimir Putin is winning so much, he's going to get tired of winning.
Date: Wed, 18 Jul 2018 05:06:58 GMT
CNN traveled to a pivotal swing state to gauge what voters there had to say about President Donald Trump's time in office, on the heels of his summit with Russian President Vladimir Putin.
Date: Wed, 18 Jul 2018 02:15:25 GMT
President Donald Trump, facing an onslaught of bipartisan fury over his glowing remarks about Vladimir Putin, said more than 24 hours afterward that he misspoke during his summit with the autocratic Russian leader.
Date: Wed, 18 Jul 2018 09:17:23 GMT
CNN's Anderson Cooper says President Donald Trump couldn't get past his attempt to clarify his remarks with Vladimir Putin without making another mistake in the process.
Date: Wed, 18 Jul 2018 01:01:48 GMT
CNN's Erin Burnett discusses President Trump's attempt to walk back his comments about Russian interference in the 2016 election by saying he misspoke.
Date: Tue, 17 Jul 2018 23:58:03 GMT
Senators in both parties said Tuesday there are a broad set of ideas under consideration to push back against President Donald Trump aligning with President Vladimir Putin at their summit in Helsinki.
Date: Tue, 17 Jul 2018 23:03:08 GMT
For the second summer in a row, President Trump stood in front of the press and demonstrated that he doesn't actually know what makes America great. The sounds of shocked gasps were audible from sea to shining sea on Monday as the US President stood next to Russian President, Vladimir Putin, in Helsinki and rebuked the US intelligence community in a breathtaking few minutes of political history.
Date: Wed, 18 Jul 2018 00:23:25 GMT
Russia announced it was ready to pursue agreements reached by Presidents Vladimir Putin and Donald Trump "in the sphere of international security," though the White House and Pentagon would not confirm any agreements had been made or offer any details.
Date: Wed, 18 Jul 2018 02:20:33 GMT
President Donald Trump seemingly questioned the United States' commitment to defending all NATO allies in an interview that aired Tuesday evening.
Date: Wed, 18 Jul 2018 03:26:00 GMT
President Donald Trump had one clear mission when he spoke to the press Tuesday afternoon before meeting with a handful of congressional Republicans: Clean up the damage he did on Monday when he broke with the intelligence community by suggesting that Russia might not have been behind the 2016 election interference and arguing that there was blame on both sides of the matter.
Date: Wed, 18 Jul 2018 09:45:12 GMT

flickr

DSC_2860.jpg

ykendler posted a photo:

DSC_2860.jpg

Date: 2018-07-18T10:11:21Z
Natural beauty

enkhee_mok posted a photo:

Natural beauty

Date: 2018-07-18T10:11:21Z
2018-07-18_12-10-14

jocerana posted a photo:

2018-07-18_12-10-14

Date: 2018-07-18T10:11:16Z
Itchy bitsy spider...

Whole Shot Motocross Photography posted a photo:

Itchy bitsy spider...

a portrait of a small wolf spider (magnification 5x)... You have got to love nature and its creations. Have a great day, my friends! via 500px ift.tt/2L1uq5x

Date: 2018-07-18T10:11:13Z
image

goldwin_kaitori posted a photo:

image

Date: 2018-07-18T10:11:16Z
Love Quotes : collaboration between www.justdoingmybe... and www.inspiredbyluc...

quotesoftheday.net posted a photo:

Love Quotes : collaboration between www.justdoingmybe... and www.inspiredbyluc...

Love Quotes :

QUOTATION – Image :


As the quote says – Description

collaboration between www.justdoingmybe…; and www.inspiredbyluc…;



quotesoftheday.net/love/love-quotes-collaboration-between...

Date: 2018-07-18T10:11:13Z
Quote of the Day - July 18, 2018

hoopoequotes posted a photo:

Quote of the Day - July 18, 2018

Quote of the Day - July 18, 2018

Quote of the Day - July 18, 2018

Education is the most powerful weapon which you can use to change the world. - Nelson Mandela



via Quote of the Day - HoopoeQuotes - HoopoeQuotes bit.ly/2zP05BU

Date: 2018-07-18T10:11:15Z
2013-05-19 16-32-35

0xluke posted a photo:

2013-05-19 16-32-35

Date: 2018-07-18T10:11:18Z
DSCF8537.jpg

Martin Zupko posted a photo:

DSCF8537.jpg

Date: 2018-07-18T10:11:22Z
10.07.2018-DSC05416.jpg

iakiyan posted a photo:

10.07.2018-DSC05416.jpg

Date: 2018-07-18T10:11:21Z

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

BB code available :

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