MIKESTOWE.COM

you are here

RSS Reader Plugin

This simple HTMLe plugin allows you to highlight PHP code by including either a small block of text, or by setting a file.

Plugin attributes:

url = the url of the feed to grab
posts
= the number of posts to show
words = the number of words to show for each post
skipfirst = skip the first number of words
endwith = end each post with (ie: … or ‘more’)
style = html code to display the posts using %title%, %description%, and %link% to refer to post characteristics.

This plugin is included in the HTMLe library by default for versions 2.0 and higher.


<?php
/*
 * HTMLe plugin
 * -------------------- 
 * File:     rssreader.php
 * Type:     function
 * Name:     rssreader
 * Purpose:  read external rss feeds
 * Author:   Michael Stowe
 * Example: {rssreader url="http://rss.cnn.com/rss/cnn_topstories.rss" posts="5" words="25" style="<strong><a href((%link%)) target='_blank'>%title%</a></strong><br />%description%<br /><br />"}
 * Required: url
 * Special: For style attribute:<ul><li>%link% = url</li><li>%title% = title</li><li>%description% = description</li></ul>
 * Advanced: {rssreader url="http://rss.cnn.com/rss/cnn_topstories.rss" posts="5" words="25" style="<strong><a href((%link%)) target='_blank'>%title%</a></strong><br />%description%<br /><br />"}
 * -------------------- 
 */
 
function HTMLe_rssreader($params$return='') {
    
$feed_url = (!empty($params['url'])?$params['url']:'http://rss.cnn.com/rss/cnn_topstories.rss');
    
$feed_number = (!empty($params['posts'])?$params['posts']:'5');
    
$wordsize = (!empty($params['words'])?$params['words']:'25');
    
$skip = (!empty($params['skipfirst'])?$params['skipfirst']:'0');
    
$endwith = (!empty($params['endwith'])?$params['endwith']:'');
    
    
$library simplexml_load_file($feed_url);
    
$i 1;
    
    foreach(
$library->channel->item as $item) {
        if(
$i <= $feed_number) {
        
            
$find = array("\n \n","\r \r","\n\n","\r\r","\n","\r","  ");
            
$replace = array("\n","\r","\n","\r",' ',' ',' ');
            
            
$words explode(" ",str_replace($find,$replace,$item->description));
            
$words array_slice($words,$skip);
            
array_splice($words,$wordsize);
        
            if(!isset(
$params['style'])) {
                
$return .= '<a href="'.$item->link.'" target="_blank" style="font-weight: strong;">'.$item->title.'</a><br />';
                
$return .= implode(" ",$words).$endwith.'<br /><br />';
            } else {
                
$key = array('%link%','%title','%description%');
                
$value = array($item->link$item->titleimplode(" ",$words).$endwith);
                
$return .= str_replace($key,$value,$params['style']);
            }
        
            
$i++;
        } else {
            break;
        }
    }
    return 
$return;
}

?>
Share this Page:
Facebook Twitter Linkedin Reddit Tumblr Email

Leave a Reply

Your email address will not be published. Required fields are marked *