Easy to use PHP RSS parsing class

Source: Internet
Author: User
Tags date array resource trim
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 ']) > m)
            {
                 $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
}
}
}

   /**
     * Date format is too much, except that the Strtotime () function in PHP can be transformed, I added another format to the recognition, the others were not written.
     */
    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);
}
}
?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.