Quick and Easy RSS Feed Reader
Quick and Easy RSS Feed Reader
RSS feeds can really be quite handy in keeping your users up to date with what's going on in the world. Many large sites with RSS feeds offer javascript code that allows users to display a list of current content on their website. This makes it easy for people with little knowlede of XML and server side scripting.
Some add RSS content to their sites for users while others for search engines. In either case javascript can fall short of perfect. Users without javascript enabled will not be able to see your content which may skew your website's layout a bit. Search engines aren't equipped yet to read text dynamically generated through a javascript script.
Two Nifty PHP Functions
For those that want a quick and easy way to display RSS feed content anywhere on there PHP enabled pages I've written some quick code. Use this code at your own risk as it's not thoroughly tested. It does make things fairly straight forward though. The code below should go in a PHP file. Something like rssfuncs.php would probably work.
<?php
#############################################
// Feed Display Functions
// By: Adam Sullivan www.hostingfanatic.com
//
// Arguments:
// $url= URL of feed
// $num= Number of items (Default 5)
// $template= Template custom code
#############################################
function display_feed($url, $num=5, $template=
"<div class=\"feeditem\"><a href=\"#link#\">
#title#</a><br />#description#</div>"){
if ($rss=@file_get_contents($url)){
$rss=xml2array($rss);
if (is_array($rss)){
$code="";
$count=0;
foreach($rss['rss'][0]['channel'][0]['item'] as $val){
$tpl=$template;
foreach($val as $key=>$val2){
$val2=html_entity_decode(htmlentities(strip_tags($val2)));
$tpl=str_replace("#".strtolower($key)."#", $val2, $tpl);
}
if ($count<$num) $code.=$tpl;
++$count;
}
}else{
return FALSE;
}
echo $code;
return TRUE;
}else{
return FALSE;
}
}
function xml2array($text) {
$reg_exp = '/<(\w+)[^>]*>(.*?)<\/\\1>/s';
preg_match_all($reg_exp, $text, $match);
foreach ($match[1] as $key=>$val) {
if ( preg_match($reg_exp, $match[2][$key]) ) {
$array[$val][] = xml2array($match[2][$key]);
} else {
$array[$val] = $match[2][$key];
}
}
return $array;
}
?>
The display_feed() function requires the use of file_get_contents(). Not a great way to go but simple enough. To include a feed on a PHP enabled web page you would need to put the following PHP code at the top of the page...
<?php
include("whatever-i-named-the-rss-functions-file.php");
?>
After that just put the display_feed() function wherever you want a feed to show up. Set the URL of the feed and, if necessary, how many items you want to be shown. The example code below uses a Hosting Fanatic feed and returns 10 results.
<?php
display_feed("http://www.hostingfanatic.com/index.rss", 10);
?>
You can style the feeds default output code through CSS via the feeditem class or you can set up a custom feed output template with the third argument of the display_feed() function. The code is pretty straight forward for that. You could copy the default value for $template out of the function declaration, change it to how you want it and then pass it as the third argument to your display_feed() call
Hopefully someone will find this useful.
Blink
Del.icio.us
Digg
Furl
Google
Simpy
Spurl
Y! MyWeb
