仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 936|回复: 19
打印 上一主题 下一主题

[学习教程] PHP网页设计很好用的php rss解析类

[复制链接]
山那边是海 该用户已被删除
跳转到指定楼层
#
发表于 2015-2-4 00:11:26 | 只看该作者 回帖奖励 |正序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
就是管理员可以编辑,删除,回复 等功能,。加入管理员功能要加入登陆系统,慢慢你会想在线添加管理员,慢慢你会让自己的作品更漂亮些,慢慢1个完整的留言板就会出来了,rss   <?php
/**
* 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");

        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']<=0)
            {
                $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<=0)
        {
            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呢?就我个人而言,完全是因为兴趣,因为我的专业和计算机完全无关,但是就是对编程很赶兴趣,尤其对网络编程、web开发特别赶兴趣。
变相怪杰 该用户已被删除
19#
发表于 2015-6-27 21:14:45 | 只看该作者
说php的话,首先得提一下数组,开始的时候我是最烦数组的,总是被弄的晕头转向,不过后来呢,我觉得数组里php里最强大的存储方法,所以建议新手们要学好数组。
柔情似水 该用户已被删除
18#
发表于 2015-6-11 02:42:47 | 只看该作者
有位前辈曾经跟我说过,phper 至少要掌握200个函数 编起程序来才能顺畅点,那些不熟悉的函数记不住也要一拿手册就能找到。所以建议新手们没事就看看php的手册(至少array函数和string函数是要记牢的)。
小魔女 该用户已被删除
17#
发表于 2015-5-2 22:05:29 | 只看该作者
曾经犯过一个很低级的错误,我在文件命名的时候用了一个横线\\\\\\\'-\\\\\\\' 号,结果找了好几个小时的错误,事实是命名的时候 是不能用横线 \\\\\\\'-\\\\\\\' 的,应该用的是下划线  \\\\\\\'_\\\\\\\' ;
飘飘悠悠 该用户已被删除
16#
发表于 2015-4-14 07:33:39 | 只看该作者
php里的数组为空的时候是不能拿来遍历的;(这个有点低级啊,不过我刚被这个边界问题墨迹了好长一会)
精灵巫婆 该用户已被删除
15#
发表于 2015-4-10 04:53:43 | 只看该作者
遇到出错的时候,我经常把错误信息直接复制到 google的搜索栏,一般情况都是能搜到结果的,不过有时候会搜出来一大片英文的出来,这时候就得过滤一下,吧中文的弄出来,挨着式方法。
兰色精灵 该用户已被删除
14#
发表于 2015-4-7 16:25:37 | 只看该作者
当留言板完成的时候,下步可以把做1个单人的blog程序,做为目标,
只想知道 该用户已被删除
13#
发表于 2015-4-1 04:08:08 | 只看该作者
我还是推荐用firefox ,配上firebug 插件调试js能省下不受时间。谷歌的浏览器最好也不少用,因为谷歌的大侠们实在是太天才啦,把一些原来的js代码加了一些特效。
不帅 该用户已被删除
12#
发表于 2015-3-27 04:10:46 | 只看该作者
本人接触php时间不长,算是phper中的小菜鸟一只吧。由于刚开始学的时候没有名师指,碰过不少疙瘩,呗很多小问题卡过很久,白白浪费不少宝贵的时间,在次分享一些子的学习的心得。
谁可相欹 该用户已被删除
11#
发表于 2015-3-26 11:13:24 | 只看该作者
基础有没有对学习php没有太大区别,关键是兴趣。
爱飞 该用户已被删除
10#
发表于 2015-3-24 23:26:44 | 只看该作者
小鸟是第一次发帖(我习惯潜水的(*^__^*) 嘻嘻……),有错误之处还请大家批评指正,另外,前些日子听人说有高手能用php写驱动程序,真是学无止境,人外有人,天外有天。
深爱那片海 该用户已被删除
9#
发表于 2015-3-23 22:26:40 | 只看该作者
在我安装pear包的时候老是提示,缺少某某文件,才发现 那群extension 的排列是应该有一点的顺序,而我安装的版本的排序不是正常的排序。没办法我只好把那群冒号加了上去,只留下我需要使用的扩展。
活着的死人 该用户已被删除
8#
发表于 2015-3-18 12:07:10 | 只看该作者
再就是混迹于论坛啦,咱们的phpchina的论坛就很强大,提出的问题一般都是有达人去解答的,以前的帖子也要多看看也能学到不少前辈们的经验。别的不错的论坛例如php100,javaeye也是很不错的。
冷月葬花魂 该用户已被删除
7#
发表于 2015-3-17 20:10:25 | 只看该作者
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
金色的骷髅 该用户已被删除
6#
发表于 2015-3-11 04:30:55 | 只看该作者
php里的数组为空的时候是不能拿来遍历的;(这个有点低级啊,不过我刚被这个边界问题墨迹了好长一会)
若天明 该用户已被删除
5#
发表于 2015-3-2 15:49:01 | 只看该作者
环境搭建好,当你看见你的浏览器输出“it works\\\\\\\"时你一定是喜悦的。在你解决问题的时候,我强烈建议多读php手册。
分手快乐 该用户已被删除
地板
发表于 2015-2-22 07:00:45 | 只看该作者
我要在声明一下:我是个菜鸟!!我对php这门优秀的语言也是知之甚少。但是我要在这里说一下php在网站开发中最常用的几个功能:
老尸 该用户已被删除
板凳
发表于 2015-2-20 07:27:51 | 只看该作者
在学习的过程中不能怕麻烦,不能有懒惰的思想。学习php首先应该搭建一个lamp环境或者是wamp环境。这是学习php开发的根本。虽然网络上有很多集成的环境,安装很方便,使用起来也很稳定、
admin 该用户已被删除
沙发
发表于 2015-2-7 03:47:09 | 只看该作者
在我安装pear包的时候老是提示,缺少某某文件,才发现 那群extension 的排列是应该有一点的顺序,而我安装的版本的排序不是正常的排序。没办法我只好把那群冒号加了上去,只留下我需要使用的扩展。
愤怒的大鸟 该用户已被删除
楼主
发表于 2015-2-4 09:55:37 | 只看该作者
有时候汉字的空格也能导致页面出错,所以在写代码的时候,要输入空格最好用引文模式。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-9-21 18:44

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表