首页 > php教程 > php手册 > 正文

很好用的php rss解析类

php中文网
发布: 2016-06-21 09:06:26
原创
1105人浏览过

rss

/**
* Rss Parse Class ver0.1
*
* @link http://www.ugia.cn/?p=42
* @author: legend (PASiOcn@msn.com)
* @version 0.1
*/

class RssParse {

    var $encoding      = "utf-8";
    var $rssurl        = "http://www.ugia.cn/wp-rss2.php";

    var $resource      = "";
    var $tag           = "";

    var $insidechannel = false;
    var $insideitem    = false;
    var $insideimage   = false;

    var $item          = array();
    var $channel       = array();
    var $image         = "";

    var $items         = array();
    var $images        = array();

    function rssReset()
    {
        $this->item    = array();
        $this->channel = array();
        $this->images  = "";
        $this->items   = array();
        $this->images  = array();
    }

    function getResource()
    {
        $fp = @fopen($this->rssurl, "rb");

        if (is_resource($fp)) {

            while($data = fread($fp, 4096)) {
                $ipd .= $data;
            }
            $this->resource = $ipd;
            @fclose($fp);

            return true;
        }

        return false;
    }

    function getEncoding()
    {
        if (preg_match('| encoding="([^"]*)"|', $this->resource, $result))
        {
            $this->encoding = strtolower($result[1]);
        }
        else
        {
            $this->encoding = "utf-8";
        }
    }

    function parseRss($rssurl = '')
    {
        if (!empty($rssurl))
        {
            $this->rssurl = $rssurl;
        }

        if (!$this->getResource())
        {
            return false;
        }

        $this->getEncoding();

        if ($this->encoding != "utf-8")
        {
            $this->resource = iconv($this->encoding, "UTF-8", $this->resource);
        }

        $xml_parser = xml_parser_create("utf-8");

        xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
        xml_set_object($xml_parser, $this);
        xml_set_element_handler($xml_parser, "startElement", "endElement");
        xml_set_character_data_handler($xml_parser, "characterData");

AppMall应用商店
AppMall应用商店

AI应用商店,提供即时交付、按需付费的人工智能应用服务

AppMall应用商店 56
查看详情 AppMall应用商店

        xml_parse($xml_parser, $this->resource, true);
        xml_parser_free($xml_parser);

        if ( count($this->channel) > 1)
        {
            $this->channel['pubdate'] = $this->mystrtotime($this->channel['pubdate']);
            if ($this->channel['pubdate']            {
                $this->channel['pubdate'] = $this->items[0]['pubdate'];
            }
        }
        return true;
    }

    function getAll()
    {
        return array(
                     'channel' => $this->channel,
                     'items'   => $this->items,
                     'images'  => $this->images
                    );
    }

    function getChannel()
    {
        return $this->channel;
    }

    function getItems()
    {
        return $this->items;
    }

    function getImages()
    {
        return $this->images;
    }

    function startElement($parser, $name, $attrs)
    {
        if ($this->insideitem || $this->insideimage || $this->insidechannel)
        {
            $this->tag = strtolower($name);
        }

        switch ($name)
        {
            case "channel" : $this->insidechannel = true; break;
            case "item"    : $this->insideitem    = true; break;
            case "image"   : $this->insideimage   = true; break;
        }
    }

    function endElement($parser, $name)
    {
        if ($name == "channel")
        {
            $this->insidechannel = false;

        }
        else if ($name == "url")
        {
            $this->images[]    = trim($this->image);
            $this->insideimage = false;
            $this->image       = "";
        }
        else if ($name == "item")
        {
            $this->item['pubdate']     = $this->mystrtotime($this->item['pubdate']);
            $this->item['description'] = trim(strip_tags($this->item['description']));
            $this->item['description'] = str_replace(" ", "", $this->item['description']);

            /**
            if (strlen($this->item['description']) > 700)
            {
                $this->item['description'] = substr($this->item['description'], 0, 697) . "…";
            }
            */

            $this->items[]         = $this->item;
            $this->item            = array();
            $this->insideitem      = false;
        }
    }

    function characterData($parser, $data)
    {
        if ($this->insideitem)
        {
            switch ($this->tag)
            {
                case "title":       $this->item['title']       .= $data; break;
                case "description": $this->item['description'] .= $data; break;
                case "link":        $this->item['link']        .= $data; break;
                case "dc:date":     $this->item['pubdate']     .= $data; break;
                case "pubdate":     $this->item['pubdate']     .= $data; break;
                case "modified":     $this->item['pubdate']     .= $data; break;
            }
        }
        elseif ($this->insideimage && $this->tag == "url")
        {
            $this->image .= $data;
        }
        elseif ($this->insidechannel)
        {
            switch ($this->tag)
            {
                case "title":         $this->channel['title']       .= $data; break;
                case "description":   $this->channel['description'] .= $data; break;
                case "link":          $this->channel['link']        .= $data; break;
                case "dc:date":       $this->channel['pubdate']     .= $data; break;
                case "pubdate":       $this->channel['pubdate']     .= $data; break;
                case "lastbuilddate": $this->channel['pubdate']     .= $data; break;
                case "modified":      $this->channel['pubdate']     .= $data; break;
            }
        }
    }

    /**
     * 日期格式太多,除了php中的strtotime()函数能够转化的,我另外加了一个格式的识别,其他的未写。
     */
    function mystrtotime($time)
    {
        $curtime = strtotime($time);
        if ($curtme        {
            if (preg_match("|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{2}:\d{2}|", $time, $result))
            {
                $time = str_replace(array("T", "+"), array(" ", " +"), $time);
                $time[23] = "";
            }

            // if (………

            $curtime = strtotime($time);
        }

        return $curtime;
    }

   function getError($msg)
   {
       die($msg);
   }
}
?>



PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号