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

UK 'unlikely to meet' migration target
EU migration has "blown off course" the UK's target to cut net migration to the tens of thousands, the home secretary says.
Date: Sun, 23 Nov 2014 16:30:49 GMT
Bomber 'kills 45' at Afghan match
A suicide bomber in east Afghanistan kills at least 45 people at a volleyball match, as Afghan MPs approve security deals with Nato and the US.
Date: Sun, 23 Nov 2014 16:22:08 GMT
Hamilton wins World Championship
Lewis Hamilton clinches his second drivers' World Championship with a comfortable victory in the season-ending Abu Dhabi Grand Prix.
Date: Sun, 23 Nov 2014 16:43:26 GMT
Abuse claims 'tip of iceberg' - May
Claims of historic sexual abuse that have emerged are the "tip of the iceberg", Home Secretary Theresa May says.
Date: Sun, 23 Nov 2014 15:07:03 GMT
New web data powers plan for police
A law forcing communications firms to keep details that could help identify criminals using the internet is being planned by the home secretary.
Date: Sun, 23 Nov 2014 16:14:26 GMT
Gordon Brown 'to stand down' as MP
Former Prime Minister Gordon Brown is to announce "within days" that he will stand down as an MP at the next election, reports suggest.
Date: Sun, 23 Nov 2014 11:05:50 GMT
Diplomats push for Iran nuclear deal
Foreign ministers from several countries try to reach a deal on Iran's nuclear programme, amid a possible extension to Monday's deadline.
Date: Sun, 23 Nov 2014 19:56:32 GMT
Band Aid single storms to number one
Band Aid 30's Do They Know It's Christmas? debuts at number one in the UK singles chart after selling more than 312,000 copies last week.
Date: Sun, 23 Nov 2014 19:59:37 GMT
New computer spying bug discovered
Computer security firm Symantec discovers what it says is one of the most sophisticated pieces of malicious software ever seen.
Date: Sun, 23 Nov 2014 18:32:53 GMT
Charing Cross reopens after fire
London's Charing Cross station reopens after a fire on a train, with some passengers describing panic and fears of a bomb.
Date: Sun, 23 Nov 2014 14:17:02 GMT

cnn

Students slam first lady over lunches
Students unhappy with school meals are taking it out on the first lady by sharing images on social media of lunches sarcastically tagged #ThanksMichelleObama.
Date: Sun, 23 Nov 2014 14:43:42 EST
'SNL' skit: Obama is Capitol bully
You sure gotta climb a lot of steps to reach the Capitol building in Washington, so just imagine what the tumble down is like.
Date: Sun, 23 Nov 2014 14:44:49 EST
Family fights village pit bull ban
A Louisiana family is fighting to protect its beloved pit bull from a "vicious dogs" ordinance.
Date: Sun, 23 Nov 2014 09:41:15 EST
WH correspondents invite 'SNL' star
Comedian and actress Cecily Strong will headline the White House Correspondents' Association annual dinner in April, the group announced Wednesday.
Date: Sun, 23 Nov 2014 15:47:08 EST
Federer wins first Davis Cup title
A year that began in uncertainty for Roger Federer ended with a historic title for the 17-time grand slam champion and his country.
Date: Sun, 23 Nov 2014 14:21:02 EST
Female sex desire found ... complicated
What makes a woman want to have sex isn't totally hormonal, as it can be with men. Happiness is key new study shows.
Date: Fri, 21 Nov 2014 13:59:44 EST
Two country singers come out as gay
There are plenty of country songs about how to be a man. One way is to face the music, and two country crooners did that on Thursday.
Date: Fri, 21 Nov 2014 07:27:25 EST
Russia: West wants regime change
With tensions simmering over the deadly crisis in Ukraine, Russia has accused the West of seeking regime change in Moscow, prompting renewed comparisons with the Cold War era.
Date: Sun, 23 Nov 2014 13:30:12 EST
'Black Widow' has five dead lovers
Police in Japan are questioning a wealthy widow about the mysterious deaths of her past partners.
Date: Fri, 21 Nov 2014 11:07:34 EST
Website streams private webcams
A Russian website is streaming video from private webcams around the world. Laurie Segall reports.
Date: Sun, 23 Nov 2014 12:17:21 EST

flickr

P1090727

christopher-amos posted a photo:

P1090727

Date: 2014-11-23T21:51:22Z
Group shot before the dance party!

Chicago Phoenix posted a photo:

Group shot before the dance party!

by klmodder instagram.com/p/vwe_ZIS_Pf/

Date: 2014-11-23T21:51:26Z

akachinesa posted a photo:

Date: 2014-11-23T21:51:27Z
A hidden gem within the huge #toyota display #landcruiser #LAAS #LAAutoshow #suv #carporn #classic #truck

engelmannmaxwell posted a photo:

A hidden gem within the huge #toyota display #landcruiser #LAAS #LAAutoshow #suv #carporn #classic #truck

Date: 2014-11-23T21:51:24Z
Kyoto-ya Japan (@KyotoyaJapan) posted a photo on Twitter

kyotoya.japan posted a photo:

Kyoto-ya Japan (@KyotoyaJapan) posted a photo on Twitter

New post on Kyoto-ya JapanLovely Miniature Kitchen Cupboard Jewellery Storage Chest made... t.co/vPnqixmhmB t.co/xJB9CM1h7a

Date: 2014-11-23T21:51:26Z
IPCam CAM1 [192.168.1.90] warning detected

seccam10301 posted a photo:

IPCam CAM1 [192.168.1.90] warning detected

This is alarm notification from your IPCam CAM1, please check back now!
Please view the camera:http://192.168.1.90:80/index.html

Date: 2014-11-23T21:51:23Z
Get it now at LA Perry www.LAPerry.com @LAPERRYFASHION @LAPERRYFASHION @LAPERRYFASHION @LAPERRYFASHION @LAPERRYFASHION @LAPERRYFASHION #dresses #coats #shoes

laperryfashion posted a photo:

Get it now at LA Perry www.LAPerry.com @LAPERRYFASHION @LAPERRYFASHION @LAPERRYFASHION @LAPERRYFASHION @LAPERRYFASHION @LAPERRYFASHION #dresses #coats #shoes

www.laperry.com

Date: 2014-11-23T21:51:26Z
#shoe #shine #qvb #Sydney

Vivarovers posted a photo:

#shoe #shine #qvb #Sydney

Date: 2014-11-23T21:51:27Z

misaelacevedo posted a photo:

Date: 2014-11-23T21:51:20Z
DSC_3350

展佑 posted a photo:

DSC_3350

Date: 2014-11-23T21:51:23Z

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

BB code available :

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