PHP生成RSS文件类实例

yipeiwu_com4年前PHP代码库

本文实例讲述了PHP生成RSS文件类文件。分享给大家供大家参考。具体如下:

PHP RSS 生成类实例代码如下:

复制代码 代码如下:
<?php
if (defined('_class_rss_php')) return;
define('_class_rss_php教程',1);
/**
 
 *  使用说明:
 *  $rss = new rss('redfox','http://jb51.net/',"redfox's blog");
 *  $rss->additem('rss class',"//www.jb51.net","xxx",date());
 *  $rss->additem(...);
 *  $rss->savetofile(...);
 */
 
class rss {
   //public
   $rss_ver = "2.0";
   $channel_title = '';
   $channel_link = '';
   $channel_description = '';
   $language = 'zh_cn';
   $copyright = '';
   $webmaster = '';
   $pubdate = '';
   $lastbuilddate = '';
   $generator = 'redfox rss generator';
 
   $content = '';
   $items = array();
 
   function rss($title, $link, $description) {
       $this->channel_title = $title;
       $this->channel_link = $link;
       $this->channel_description = $description;
       $this->pubdate = date('y-m-d h:i:s',time());
       $this->lastbuilddate = date('y-m-d h:i:s',time());
   }
 
   function additem($title, $link, $description ,$pubdate) {
       $this->items[] = array('titile' => $title ,
                        'link' => $link,
                        'description' => $description,
                        'pubdate' => $pubdate);
   }
 
   function buildrss() {
       $s = "<!--l version="1.0" encoding="gb2312"--> ";
       // start channel
       $s .= " ";
       $s .= " "
       $s .= "<link />{$this->channel_link} ";
       $s .= "{$this->channel_description} ";
       $s .= "{$this->language} ";
       if (!emptyempty($this->copyright)) {
          $s .= "{$this->copyright} ";
       }
       if (!emptyempty($this->webmaster)) {
          $s .= "{$this->webmaster} ";
       }
       if (!emptyempty($this->pubdate)) {
          $s .= "{$this->pubdate} ";
       }
 
       if (!emptyempty($this->lastbuilddate)) {
          $s .= "{$this->lastbuilddate} ";
       }
 
       if (!emptyempty($this->generator)) {
          $s .= "{$this->generator} ";
       }
      
       // start items
       for ($i=0;$iitems),$i++) {
           $s .= " ";
           $s .= " ";
           $s .= "<link />{$this->items[$i]['link']} ";
           $s .= "<!--data[{$thi-->items[$i]['description']}]]> ";
           $s .= "{$this->items[$i]['pubdate']} ";          
           $s .= " ";
       }
     
      // close channel
      $s .= " ";
      $this->content = $s;
   }
 
   function show() {
       if (emptyempty($this->content)) $this->buildrss();
       header('content-type:text/xml');
       echo($this->content);
   }
 
   function savetofile($fname) {
       if (emptyempty($this->content)) $this->buildrss();
       $handle = fopen($fname, 'wb');
       if ($handle === false)  return false;
       fwrite($handle, $this->content);
       fclose($handle);
   }
}
?>

希望本文所述对大家的PHP程序设计有所帮助。

相关文章

浅析51个PHP处理字符串的函数

包括,计算字符串长度、分割字符串、查找字符串等等各个方面。1.AddSlashes: 字符串加入斜线。 2.bin2hex: 二进位转成十六进位。 3.Chop: 去除连续空白。 4.C...

PHP基于socket实现的简单客户端和服务端通讯功能示例

本文实例讲述了PHP基于socket实现的简单客户端和服务端通讯功能。分享给大家供大家参考,具体如下: 服务器端: <?php set_time_limit(0);...

PHP数字前补0的自带函数sprintf 和number_format的用法(详解)

很多时候我们需要对数字进行格式化,比如位数不足前面加0补足。用PHP可以很轻易实现,因为PHP自带了相关功能的函数。 <?php //生成4位数,不足前面补0...

php输出含有“#”字符串的方法

本文实例讲述了php输出含有“#”字符串的方法。分享给大家供大家参考,具体如下: 因为#在php中是注释,无法正常输出,需要转换和处理。 输出页: <? functi...

PHP 文件上传源码分析(RFC1867)

你总不至于在用户要上传头像的时候告诉用户”请打开FTP客户端,上传文件到//www.jb51.net/uploads/中, 并以2dk433423l.jpg命名”吧? 而基于HTTP的...