日期:2012-05-23  浏览次数:20489 次

php源代码及代码详细解释如下:

<?php  //RSS源地址列表数组  $rssfeed = array("http://www.jzxue.com/rss/12.xml",  "http://rss.sina.com.cn/news/allnews/sports.xml",  "http://ent.163.com/special/00031K7Q/rss_toutiao.xml",  "http://tech.163.com/special/00091JPQ/techimportant.xml");     //设置编码为UTF-8  header('Content-Type:text/html;charset= UTF-8');          for($i=0;$i<sizeof($rssfeed);$i++){//分解开始      $buff = "";      $rss_str="";      //打开rss地址,并读取,读取失败则中止      $fp = fopen($rssfeed[$i],"r") or die("can not open $rssfeed");       while ( !feof($fp) ) {          $buff .= fgets($fp,4096);      }      //关闭文件打开      fclose($fp);                //建立一个 XML 解析器      $parser = xml_parser_create();      //xml_parser_set_option -- 为指定 XML 解析进行选项设置      xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);      //xml_parse_into_struct -- 将 XML 数据解析到数组$values中      xml_parse_into_struct($parser,$buff,$values,$idx);      //xml_parser_free -- 释放指定的 XML 解析器      xml_parser_free($parser);            foreach ($values as $val) {          $tag = $val["tag"];          $type = $val["type"];          $value = $val["value"];          //标签统一转为小写          $tag = strtolower($tag);                    if ($tag == "item" && $type == "open"){              $is_item = 1;          }else if ($tag == "item" && $type == "close") {              //构造输出字符串              $rss_str .= "<a href='".$link."' target=_blank>".$title."</a><br />";              $is_item = 0;          }          //仅读取item标签中的内容          if($is_item==1){              if ($tag == "title") {$title = $value;}                      if ($tag == "link") {$link = $value;}          }      }      //输出结果      echo $rss_str."<br />";  }  ?>