Skip to content

Instantly share code, notes, and snippets.

@danyboy666
Forked from zma/rss2html.php
Created September 1, 2019 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danyboy666/05927735a26d2cd93373c050e1108650 to your computer and use it in GitHub Desktop.
Save danyboy666/05927735a26d2cd93373c050e1108650 to your computer and use it in GitHub Desktop.
rss2html.php
<?php
// Check http://www.systutorials.com/136102/a-php-function-for-fetching-rss-feed-and-outputing-feed-items-as-html/ for description
// RSS to HTML
/*
$tiem_cnt: max number of feed items to be displayed
$max_words: max number of words (not real words, HTML words)
if <= 0: no limitation, if > 0 display at most $max_words words
*/
function get_rss_feed_as_html($feed_url, $max_item_cnt = 10, $show_date = true, $show_description = true, $max_words = 0, $cache_timeout = 7200, $cache_prefix = "/tmp/rss2html-")
{
$result = "";
// get feeds and parse items
$rss = new DOMDocument();
$cache_file = $cache_prefix . md5($feed_url);
// load from file or load content
if ($cache_timeout > 0 &&
is_file($cache_file) &&
(filemtime($cache_file) + $cache_timeout > time())) {
$rss->load($cache_file);
} else {
$rss->load($feed_url);
if ($cache_timeout > 0) {
$rss->save($cache_file);
}
}
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'content' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
$content = $node->getElementsByTagName('encoded'); // <content:encoded>
if ($content->length > 0) {
$item['content'] = $content->item(0)->nodeValue;
}
array_push($feed, $item);
}
// real good count
if ($max_item_cnt > count($feed)) {
$max_item_cnt = count($feed);
}
$result .= '<ul class="feed-lists">';
for ($x=0;$x<$max_item_cnt;$x++) {
$title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$result .= '<li class="feed-item">';
$result .= '<div class="feed-title"><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong></div>';
if ($show_date) {
$date = date('l F d, Y', strtotime($feed[$x]['date']));
$result .= '<small class="feed-date"><em>Posted on '.$date.'</em></small>';
}
if ($show_description) {
$description = $feed[$x]['desc'];
$content = $feed[$x]['content'];
// find the img
$has_image = preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $content, $image);
// no html tags
$description = strip_tags(preg_replace('/(<(script|style)\b[^>]*>).*?(<\/\2>)/s', "$1$3", $description), '');
// whether cut by number of words
if ($max_words > 0) {
$arr = explode(' ', $description);
if ($max_words < count($arr)) {
$description = '';
$w_cnt = 0;
foreach($arr as $w) {
$description .= $w . ' ';
$w_cnt = $w_cnt + 1;
if ($w_cnt == $max_words) {
break;
}
}
$description .= " ...";
}
}
// add img if it exists
if ($has_image == 1) {
$description = '<img class="feed-item-image" src="' . $image['src'] . '" />' . $description;
}
$result .= '<div class="feed-description">' . $description;
$result .= ' <a href="'.$link.'" title="'.$title.'">Continue Reading &raquo;</a>'.'</div>';
}
$result .= '</li>';
}
$result .= '</ul>';
return $result;
}
function output_rss_feed($feed_url, $max_item_cnt = 10, $show_date = true, $show_description = true, $max_words = 0)
{
echo get_rss_feed_as_html($feed_url, $max_item_cnt, $show_date, $show_description, $max_words);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment