<?php
global $indexval;
function startElement($parser,$tagName,$attrs){
global $insideitem,$tag;
if($insideitem){
$tag=$tagName;
}
elseif($tagName=="ITEM"){
$insideitem = true;
}
}
function characterData($parser,$data){
global $insideitem,$tag,$title,$description,$link;
if($insideitem&&$indexVal<5){
switch($tag){
case "TITLE":
$indexVal++;
$title.=$data;
break;
case "DESCRIPTION":
$description.=$data;
break;
case "LINK":
$link.=$data;
break;
}
}
}
function endElement($parser,$tagName){
global $insideitem,$tag,$title,$description,$link;
if($tagName=="ITEM"&&$indexVal<5){
printf("<p><b><a href='%s'>%s</a></b></p>",$link,$title); //trim($link) htmlspecialchars($title)
printf("<p>%s</p>",$description);
$title="";
$description="";
$link="";
$insideitem=false;
}
}
$xml_parser=xml_parser_create(); //Create an XML parser
xml_set_element_handler($xml_parser,"startElement","endElement"); //Set the functions to handle opening and closing tags
xml_set_character_data_handler($xml_parser,"characterData"); //Set the function to handle blocks of character data
$fp=fopen("http://www.pixel2life.com/feeds/gimp.xml","r") //Open the XML file for reading http://www.sitepoint.com/rss.php
or die("Error reading RSS data.");
while($data=fread($fp,4096)) //Read the XML file 4KB at a time
xml_parse($xml_parser, $data, feof($fp)) //Parse each 4KB chunk with the XML parser created above
or die(sprintf("XML error: %s at line %d", //Handle errors in parsing
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp); //Close the XML file
xml_parser_free($xml_parser); //Free up memory used by the XML parser
?>
I'm trying to use a global variable to limit the number of displayed topics from an RSS feed.
Can someone explain to me what I'm doing wrong?
I'm fairly new to php.