[PHP] Reading XML. The code works but I need to edit a simple foreach loop?

This is puzzling my and I had to share it now with the good people on here. I was on a another PHP dev site and I didn’t get any help, that was for another problem which I solved myself. So in reward for fixing one problem on my own, I hoped I could ask for the help of the fine people on a cough, better, site to hope I can find a answer to my woes!

This is my code, it’s a bit long, but it essentially reads from a RSS feed, puts the data into an array and spits it out onto the page.

I’ve spent hours trying to just get it to render the first 5 articles in the XML but it insists on not listening to me. I’ve put if/do/while counters all over the place and nothing likes it. I’ve tried to count the array and it’s always 1 so I seem to be unable to array_slice it too.

I really hope someone can shed some light on this, I might go nuts otherwise :rabbit:


$rssFeeds = 'http://www.pharmatimes.com/RSS/WorldNewsFeed.aspx';

foreach (array($rssFeeds) as $feed) {


function startElement($xp,$name,$attributes) { 
global $item,$currentElement;  
$currentElement = $name; 
//this part makes sure the content is being parsed from a section of the feed 
//containing the data we want
if ($currentElement == 'ITEM') { 
//by default PHP converts everything to uppercase    
$item = true; 
// We're only interested in the contents of the item element. This flag keeps track of where we 


function endElement($xp,$name) {  
global $item,$currentElement,$title,$description,$link;    
if ($name == 'ITEM') { 
// If we're at the end of the item element, display 
// the data, and reset the globals
echo "<b>Title:</b> $title<br>";    
echo "<b>Description:</b> $description <a href=content.php?link=$link target='mainFrame'>Read 


$title = '';    
$description = '';    
$link = '';    
$item = false;  

function characterDataHandler($xp,$data) {  
global $item,$currentElement,$title,$description,$link; 

if ($item) { 
//Only add to the globals if we're inside an item element.    
switch($currentElement) {
case "TITLE":        
$title .= $data; 
// We use .= because this function may be called multiple times for one element.        
case "DESCRIPTION":        
case "LINK":        
     }  }}

function readFeeds($feed) {
  $fh = fopen($feed,'r'); 
// open file for reading

  $xp = xml_parser_create(); 
// Create an XML parser resource

  xml_set_element_handler($xp, "startElement", "endElement"); 
// defines which functions to call when element started/ended
  xml_set_character_data_handler($xp, "characterDataHandler");

  while ($data = fread($fh, 4096)) {
    if (!xml_parse($xp,$data)) {
      return 'Error in the feed';


thanks in advance,