[PHP] RSS to Array function

<?php
        
        $rss_tags = array(
                'title',
                'link',
                'guid',
                'comments',
                'description',
                'pubDate',
                'category',
        );
        $rss_item_tag = 'item';
        $rss_url = 'http://www.pixel2life.com/feeds/adobe_fireworks.xml';
        
        $rssfeed = rss_to_array($rss_item_tag,$rss_tags,$rss_url);
        
        echo '<pre>';
        print_r($rssfeed);

        function rss_to_array($tag, $array, $url) {
                $doc = new DOMdocument();
                $doc->load($url);
                $rss_array = array();
                $items = array();
                foreach($doc->getElementsByTagName($tag) AS $node) {    
                        foreach($array AS $key => $value) {
                                $items[$value] = $node->getElementsByTagName($value)->item(0)->nodeValue;
                        }
                        array_push($rss_array, $items);
                }
                return $rss_array;
        }
?>

 

posted @ 2012-04-16 22:15  Lux.Y  阅读(238)  评论(0)    收藏  举报