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).


Shapps quits amid Tory bullying claims
Grant Shapps quits as a minister amid claims he failed to act on allegations of bullying in the Tory youth wing, saying the "buck should stop with me".
Date: Sat, 28 Nov 2015 17:00:26 GMT
Russia announces Turkey sanctions
Russia announces a package of economic sanctions against Turkey over the shooting down of a Russian jet on the Syrian border on Tuesday.
Date: Sat, 28 Nov 2015 19:18:09 GMT
Vardy breaks goalscoring record
Jamie Vardy breaks Ruud van Nistelrooy's record as he becomes the first player to score in 11 consecutive Premier League matches.
Date: Sat, 28 Nov 2015 18:37:40 GMT
Tories lobby Labour MPs on Syria
Senior ministers phone opposition MPs to build support for possible air strikes in Syria, as Stop the War protests are held in London.
Date: Sat, 28 Nov 2015 19:05:32 GMT
Hundreds more jobs saved at Caparo
A further 333 jobs are saved at steel company Caparo, administrators PwC announce.
Date: Sat, 28 Nov 2015 13:48:27 GMT
Mass Yazidi grave found in north Iraq
A booby-trapped mass grave feared to hold more than 100 bodies is found near Sinjar, northern Iraq, after the town was freed from IS fighters.
Date: Sat, 28 Nov 2015 17:36:49 GMT
GB lead after Murrays win doubles
Britain are one win from a first Davis Cup title in 79 years after Andy and Jamie Murray's doubles victory against Belgium.
Date: Sat, 28 Nov 2015 16:59:46 GMT
'High chance' of chamber in Tut's tomb
Egyptian officials say they now think it is "90% likely" that there is a hidden chamber in King Tutankhamun's tomb.
Date: Sat, 28 Nov 2015 11:48:50 GMT
Three die in Colorado clinic shooting
A shooting at a family planning clinic in the US town of Colorado Springs leaves two civilians and a policeman dead, with the suspected gunman arrested.
Date: Sat, 28 Nov 2015 14:49:51 GMT
Amazon UK reports biggest sales day
Online retailer Amazon says it has its biggest sales day ever in the UK as shoppers rush to buy bargains on Black Friday.
Date: Sat, 28 Nov 2015 13:12:47 GMT


Motive remains elusive in Planned Parenthood shooting

Date: Sat, 28 Nov 2015 19:25:12 GMT
Director, studio: Sorry for cast choice
The makers of the forthcoming film "Gods of Egypt" apologized for showcasing a predominantly white cast amid criticism over lack of diversity in a film based on Egyptian mythology.

Date: Sat, 28 Nov 2015 19:21:06 GMT
Upcoming CNN Short Films
CNN Films and CNN Digital Studios present original short films directed by established and rising filmmakers.

Date: Sat, 28 Nov 2015 18:14:45 GMT
Preview short film 'Blackface'
African-American filmmaker Roger Ross Williams goes on a journey to understand why the notoriously liberal Dutch feel compelled to dress in blackface during the annual holiday tradition of Sinterklaas. How can a country that boasts of a multi-cultural society and liberal attitude be so oblivious of its own colonial past? Premieres Monday, Nov. 30, on CNN Digital.

Date: Sat, 28 Nov 2015 18:14:45 GMT
Deaf football team captures the hearts of thousands
"All-American Family" tells the story of how a high school football team made up of deaf players has captured the hearts and minds of an entire town. Directed by Andrew Jenks.

Date: Sat, 28 Nov 2015 18:14:45 GMT
Erdogan 'saddened' by downing
Turkish President Recep Tayyip Erdogan said Saturday that his government is "really saddened" about shooting down a Russian warplane, insisting that he does not want to ramp up tensions but falling short of offering the apology Moscow has demanded.

Date: Sat, 28 Nov 2015 17:58:41 GMT
... shootout caught on video
Tahir Elci, the President of Diyarbakir Bar Association and prominent pro-Kurdish attorney, was shot and killed in the predominantly Kurdish city of Diyarbakir.

Date: Sat, 28 Nov 2015 17:54:52 GMT
Pro-Kurdish lawyer killed on street
Pro-Kurdish lawyer Tahir Elci, president of the Diyarbakir Bar Association in southeastern Turkey and a leading human rights defender, was killed while making a press statement Saturday.

Date: Sat, 28 Nov 2015 17:53:52 GMT
Robber shoots student, but then ...
A robber shot student Peter Gold in the stomach after he confronted a suspect. When the suspect tried to shoot Gold again, the gun jammed.

Date: Sat, 28 Nov 2015 17:52:04 GMT
Tracy Morgan: I went to the other side
Tracy Morgan says his life will never be the same after a near-fatal car crash in June 2014 -- and that's not necessarily a bad thing.

Date: Sat, 28 Nov 2015 17:24:19 GMT



esbol posted a photo:


Date: 2015-11-28T20:10:52Z
Judy Costa and her Grand-Daughter Chole Haven Sciacca.

Fox Point Photo History posted a photo:

Judy Costa and her Grand-Daughter Chole Haven Sciacca.

Judy Costa and her Grand-Daughter Chole Haven Sciacca.

Date: 2015-11-28T20:10:53Z

JacsPhotoArt posted a photo:


Date: 2015-11-28T20:10:56Z

bcolasanto posted a photo:

Date: 2015-11-28T20:10:54Z
Dursley Island Cable Car

markbuckley1 posted a photo:

Dursley Island Cable Car

Date: 2015-11-28T20:10:56Z

TorbenKeller posted a photo:


Date: 2015-11-28T20:10:56Z

Zard tony posted a photo:


Date: 2015-11-28T20:10:57Z

carles oviedo posted a photo:


Date: 2015-11-28T20:10:59Z
TVR 101 Open day

Colin RedGriff posted a photo:

TVR 101 Open day

Date: 2015-11-28T20:10:57Z

Euridice2 posted a photo:


Date: 2015-11-28T20:10:58Z


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

BB code available :

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