RSS/XML feed parser

Here's some php:

function xml_parser($page,$container,$tags,$number,$cdata) {
  if (!$number) {$number=100;}
  preg_match_all("/<$container>.+<\/$container>/sU",$xml, $items);
   foreach ($items as $item) {
    for($i=0; $i<count($tags); $i++) {
    preg_match("/<$tags[$i](.+)(<\/$tags[$i]>)/sU", $item, $tag);
    $this[$i]=array_map('html_entity_decode', $this[$i]);
     if (count($itemsArray)<$number) {array_push($itemsArray, $this);}
  foreach ($itemsArray as $item) {
  for($i=0; $i<count($tags); $i++) {
  $data[$i]=$item[$i][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]='';
   <dt><a href=\"$link\">$title</a></dt>
   <dd class=\"story\">$description</dd>
   <dd>Date: $date</dd>\r";
return $theData;



Here's some HTML with PHP

<?php echo $bbc; ?>
<?php echo $cnn; ?>
<?php echo $flickr; ?>

Here's what we get... (the lastest feeds from the BBC, CNN, and flickr).


Missile confirmed to have downed MH17
The Dutch Safety Board confirms that a Russian-made missile brought down Malaysian Airlines flight MH17 over Ukraine in 2014, killing 298 people.
Date: Tue, 13 Oct 2015 14:35:41 GMT
McDonnell: I changed tactics, not policy
John McDonnell says he "changed my mind" on tactics - not policy - after a U-turn sparks criticism within Labour.
Date: Tue, 13 Oct 2015 15:22:39 GMT
UK pulls out of £5.9m Saudi jail deal
The UK government withdraws from a controversial £5.9m deal to provide expertise to Saudi Arabia's prison service.
Date: Tue, 13 Oct 2015 16:06:10 GMT
UK inflation rate turns negative again
Inflation as measured by Consumer Prices Index fell to -0.1% in September, partly due to falling fuel prices, official figures show.
Date: Tue, 13 Oct 2015 14:01:42 GMT
Boy shooting 'linked to gang feuds'
The shooting of a seven-year-old boy and his mother at their front door is linked to gang feuds in part of Greater Manchester, police say.
Date: Tue, 13 Oct 2015 15:27:17 GMT
Three killed in Jerusalem attacks
Three Israelis have been killed and more than 20 injured in shooting and stabbing attacks in Jerusalem and central Israel, Israeli police say.
Date: Tue, 13 Oct 2015 15:29:03 GMT
Minister refuses to give refugee figure
Minister for refugees Richard Harrington refuses to tell MPs how many Syrian refugees are in Britain, saying he will not give a "running commentary" on numbers.
Date: Tue, 13 Oct 2015 16:06:25 GMT
PM to write over Saudi alcohol case
David Cameron will write to the Saudi Arabian government about the case of a UK pensioner imprisoned for possessing alcohol, Downing Street says.
Date: Tue, 13 Oct 2015 14:46:41 GMT
Becky Watts jury visits her home
The jury in the Becky Watts murder trial visits her Bristol home.
Date: Tue, 13 Oct 2015 13:49:55 GMT
Loyalists 'left behind' since 1998
Loyalists were left behind economically and ignored politically since the Good Friday Agreement, Tony Blair's former chief of staff has said.
Date: Tue, 13 Oct 2015 16:38:29 GMT


Families release fatal crash video
The families of two men killed in a fatal car crash while driving under the influence released a video recorded moments before the crash in an attempt to prevent future tragedies.

Date: Tue, 13 Oct 2015 16:12:23 GMT
Sanders throws down gauntlet for Clinton on Wall Street, Iraq

Date: Tue, 13 Oct 2015 16:06:30 GMT
How tiny nation's big dream came true
It's not often a prime minister personally takes your call -- but then again it's not often Albania qualifies for one of the world's top football tournaments.

Date: Tue, 13 Oct 2015 15:49:45 GMT
Putin: Syria strikes protect the world
After four years and 250,000 deaths, the Syrian civil war is getting even more complicated. And now the U.S. and Russia are stepping up their roles in the country -- though on opposing sides.

Date: Tue, 13 Oct 2015 15:35:15 GMT
MH17 passengers' final moments revealed
[Breaking news update posted at 7:25 a.m. ET]

Date: Tue, 13 Oct 2015 15:28:22 GMT
3 killed in new Israel violence
Israeli-Palestinian tensions escalated as four violent attacks reverberated across the nation Tuesday morning.

Date: Tue, 13 Oct 2015 15:07:24 GMT
What man does with sharks will terrify you
Dive into the shark-filled waters off South Africa's coast and get a first-hand look at an often misunderstood creature.

Date: Tue, 13 Oct 2015 14:44:01 GMT
What's up beneath London?

Date: Tue, 13 Oct 2015 14:39:39 GMT
What a shot! 39 amazing sports photos

Date: Tue, 13 Oct 2015 14:29:49 GMT
'Ruin porn': The 24 buildings you must see
What are we to make of "ruin porn", the work of photographers and artists who aim to communicate the romantic frisson of run-down buildings?

Date: Tue, 13 Oct 2015 13:41:28 GMT



ivyzmommy posted a photo:


Date: 2015-10-13T16:41:37Z

taitien1 posted a photo:

Date: 2015-10-13T16:41:37Z
43-2015-10-02 TaroTaroStory-_DSC3182.jpg

Circolo Arci Fuori Orario posted a photo:

43-2015-10-02 TaroTaroStory-_DSC3182.jpg

Date: 2015-10-13T16:41:39Z

Lionel Ferette posted a photo:


Date: 2015-10-13T16:41:41Z

kaoru42_99 posted a photo:


Date: 2015-10-13T16:41:40Z

ashcodmeg posted a photo:


Date: 2015-10-13T16:41:40Z
campeonato-baiano-xadrez-2015 (50) posted a photo:

campeonato-baiano-xadrez-2015 (50)

Date: 2015-10-13T16:41:41Z

lena.albach posted a photo:


Date: 2015-10-13T16:41:41Z
Вести Дегтярска / Degtyarsk, Russia

borisovodov posted a photo:

Вести Дегтярска / Degtyarsk, Russia

Date: 2015-10-13T16:41:38Z

Marenko - Eyes of Holland posted a photo:

Date: 2015-10-13T16:41:39Z


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.

2007-03-02 BonRouge says :

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.

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.

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.

2007-03-07 BonRouge says :

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?

2007-03-10 BonRouge says :

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?

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

2007-11-02 steve says :

thanks sorted out my cdata parasing problem, seems that is not too clear in the docs


Comment form

Please type the word 'coffee' here:

BB code available :

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