I'm using the following code to parse and print Yahoo Sports News. Does anybody know how to filter the news. So instead of receiving the full news listings my page only shows sports news about a certain city. Any ideas. Thanks?
<?php
$item = array();
$depth = array();
$_tags = array("dummy");
function initArray()
{
global $_item;
$_item = array ("TITLE"=>"", "LINK"=>"",
"DESCRIPTION"=>"", "URL"=>"");
}
function startElement($parser, $name){
global $depth, $tags, $_item;
if (($name=="ITEM") ||
($name=="CHANNEL")
|| ($name=="IMAGE")) {
initArray();
}
@$depth[$parser]++;
array_push($tags, $name);
}
function endElement($parser, $name){
global $depth, $tags, $_item;
array_pop($tags);
$depth[$parser]--;
switch ($name) {
case "ITEM":
echo "<p><a href=\"{$item['LINK']}
\">" .
"{$item['TITLE']}</a></p>\n";
initArray();
break;
case "IMAGE":
echo "<a href=’{$item['LINK']}’>" .
"<DEFANGED_IMG src=’{$item
['URL']}’ " .
"alt=’{$_item['TITLE']};
border=’0’></a>\n<br />\n";
initArray();
break;
case "CHANNEL":
echo "<h3>{$_item['TITLE']}</h3>\n";
initArray();
break;
}
}
function parseData($parser, $text){
global $depth, $tags, $_item;
$crap = preg_replace ("/\s/", "", $text);
/ is the data just whitespace?
if so, we don't want it! /
if ($crap) {
$text = preg_replace ("/\s+/", "", $text);
/ get rid of leading whitespace /
if (@$item[$tags[$depth[$parser]]]) {
$item[$tags[$depth[$parser]]] .=
$text;
} else {
$item[$tags[$_depth[$parser]]] =
$text;
}
}
}
function parseRDF($file){
global $depth, $tags, $_item;
$xml_parser = xml_parser_create();
initArray();
/ Set up event handlers /
xml_set_element_handler
($xml_parser, "startElement", "endElement");
xml_set_character_data_handler
($xml_parser, "parseData");
/ Open up the file /
$fp = fopen ($file, "r") or die ("Could not
open $file for input");
while ($data = fread ($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof
($fp))) {
die (sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code
($xml_parser)),
xml_get_current_line_number
($xml_parser)));
}
}
fclose($fp);
xml_parser_free($xml_parser);
}
parseRDF
("http://rss.news.yahoo.com/rss/sports");
?>