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

SNP celebrate - as Labour 'hang on'
Nicola Sturgeon plays down talk of an independence referendum after the SNP's win - while Jeremy Corbyn says results across Britain show Labour had "hung on".
Date: Fri, 06 May 2016 15:03:16 GMT
Labour just short as UKIP wins seats
Labour falls short of a majority in the Welsh Assembly as UKIP wins its first seats in Cardiff Bay.
Date: Fri, 06 May 2016 16:21:43 GMT
Sadiq Khan leads London mayoral vote
Labour candidate Sadiq Khan is leading the London mayoral elections race, provisional counting shows.
Date: Fri, 06 May 2016 16:38:30 GMT
Government U-turn over forced academies
Education Secretary Nicky Morgan announces the government will not now pursue plans to force all schools in England to become academies.
Date: Fri, 06 May 2016 16:04:49 GMT
'Boaty' ship named after Attenborough
The UK's new polar research ship is to be named RRS Sir David Attenborough, despite Boaty McBoatface previously topping a public vote.
Date: Fri, 06 May 2016 10:32:23 GMT
Panama Papers source breaks silence
The source behind the leak of the Panama Papers speaks for the first time, offering to help with prosecutions in return for immunity.
Date: Fri, 06 May 2016 16:35:39 GMT
Rural broadband 'only on request'
There will be no automatic rollout of broadband to homes and businesses in the remotest parts of the UK, the government says.
Date: Fri, 06 May 2016 14:01:16 GMT
Cologne sex attack charge dismissed
A judge in the German city of Cologne dismisses sexual assault charges against an Algerian man, in the first trial over sexual crimes at New Year celebrations.
Date: Fri, 06 May 2016 14:53:49 GMT
On-the-run killing suspect detained
A man who went on the run suspected of being armed after a fatal shooting is arrested after he hands himself in to police.
Date: Fri, 06 May 2016 15:44:47 GMT
Go your own way, Erdogan tells EU
Turkey's president tells the EU it will not change its anti-terror laws in return for visa-free travel, saying "we'll go our way, you go yours".
Date: Fri, 06 May 2016 15:10:37 GMT

cnn

1,500 vehicles packed with evacuees flee wildfire
At least 88,000 people have been forced to flee the fires in Fort McMurray, Canada.

Date: Fri, 06 May 2016 11:25:58 EDT
How to help the wildfire evacuees
Wildfires have destroyed at least 1,600 homes in Alberta, Canada, and forced the mandatory evacuation of 88,000 people -- including the entire city of Fort McMurray.
Date: Fri, 06 May 2016 11:14:01 EDT
'It was driving through hell'
Michel Chamberland narrowly escaped the wildfire engulfing the Canadian city of Fort McMurray. He captured this footage while evacuating.
Date: Fri, 06 May 2016 10:34:09 EDT
Fort McMurray fire: What we know
Days after residents escaped a roaring wildfire in the Canadian city of Fort McMurray, the blaze shows no signs of abating.

Podcast favorites: Blindsided: How ISIS Shook The World | Bernie gets the Axe | Guns in America

Date: Fri, 06 May 2016 11:22:36 EDT
Fierce resistance to Trump
Date: Fri, 06 May 2016 10:05:47 EDT
How Ted Cruz was out-outsidered
Ted Cruz was always the Washington outsider. He just wasn't the Washington outsider Republicans wanted.
Date: Fri, 06 May 2016 11:20:53 EDT
Marines sent to U.S. Embassy
Twenty-five additional heavily armed U.S. Marines arrived at the U.S. embassy in Baghdad within the last several hours, a U.S. official tells CNN.

Date: Fri, 06 May 2016 11:38:58 EDT
Russia flexes its military might in Syria
Despite Russia's claim that it would withdraw most of its forces from Syria, they maintain a strong presence at the Hmeymim airbase near Latakia.
Date: Fri, 06 May 2016 04:09:06 EDT
Teen girl strangled, drugged, burned
More than a dozen people have been arrested after a teenage girl was choked, injected with poison, tied to a van and then burned to death.
Date: Fri, 06 May 2016 11:25:47 EDT
N. Korea: Once-in-generation event
North Korea, one of the world's most secretive nations, is putting on its biggest political event in 36 years.

Popular: Guns in America | Sanders Demands Clinton Apologize | Blindsided: How ISIS Shook The World

Date: Fri, 06 May 2016 11:30:49 EDT

flickr

#مصطلحات #مهمة #في #صعوبات_التعلم #اسامة_مدبولي #osama_madbooly #learning_disabilities #awareness

tafaolcenter posted a photo:

#مصطلحات #مهمة #في #صعوبات_التعلم #اسامة_مدبولي #osama_madbooly #learning_disabilities #awareness

Date: 2016-05-06T16:47:04Z

lizfitz78 posted a photo:

Date: 2016-05-06T16:47:05Z
20160506_145605

Willow O'B Long posted a photo:

20160506_145605

Date: 2016-05-06T16:47:06Z
IMG_4600

jalcabes posted a photo:

IMG_4600

Date: 2016-05-06T16:47:08Z
Critical time for insurance industry to get involved with automated vehicles

asif_ashi2001 posted a photo:

Critical time for insurance industry to get involved with automated vehicles

Critical time for insurance industry to get involved with automated vehicles
Insurance Institute report says industry could be champion for safety benefits
Jeff Pearce on May 6, 2016

A new report put out by the Insurance Institute warns that the insurance industry has about 10 years as regular...

autoinsurancereview.tk/2016/05/06/critical-time-for-insur...

Date: 2016-05-06T16:47:05Z
20160504184627_IMG_1629

saramatías posted a photo:

20160504184627_IMG_1629

Date: 2016-05-06T16:47:07Z
Sunset skyline across the river #sunset #skyline #river #urban #explore

Johannes Drooghaag posted a photo:

Sunset skyline across the river  #sunset #skyline #river #urban #explore

Date: 2016-05-06T16:47:02Z
Những ngày tháng 4

thangtlt posted a photo:

Những ngày tháng 4

Date: 2016-05-06T16:47:06Z
كاسيميرو يودع اربيلوا

ودي بك posted a photo:

كاسيميرو يودع اربيلوا

ift.tt/1SUjS0z

Date: 2016-05-06T16:47:08Z

rubyvu1 posted a photo:

Date: 2016-05-06T16:47:02Z

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

BB code available :

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