Display Excerpts From an RSS Feed, with Pagination
A PHP5 script that reads an RSS file and outputs short customisable excerpts to HTML, with 'read more' links leading to the full story. Pagination is included to break a long RSS file into smaller pieces and limit the number of items shown per page.
Works in modern browsers and IE7+
$count) { $words = array_slice($words, 0, $count); $string = implode(' ', $words).'...'; } return $string; } function showPagination($page, $page_count, $range) { // pagination echo ''."\n";
echo 'Page(s): '."\n";
if ($page > 1) {
echo '<< < | ';
}
for ($num = ($page - $range); $num < (($page + $range) + 1); $num++) {
if($num > 0 && $num <= $page_count) {
if($num == $page) {
echo ' '.$num.' |';
} else {
echo ' '.$num.' |';
}
}
}
if ($page != $page_count) {
echo ' > >> ';
}
echo "\n
\n";
}
$xml = simplexml_load_file($rss_file);
$item_count = count($xml->channel->item);
$page_count = ceil($item_count / $per_page);
$page = (isset($_GET['page']) && is_numeric($_GET['page'])) ? (int)$_GET['page'] : 1;
if ($page > $page_count) { $page = $page_count; }
if ($page < 1) { $page = 1; }
$items = ($item_count < ($page * $per_page)) ? $item_count : $page * $per_page;
echo ''.$rss_title.'
Showing '.($page * $per_page - $per_page + 1).'-'.$items.' of '.$item_count.' items:
'."\n"; $remaining = $per_page - (($page * $per_page) - $item_count); $offset = (($page - 1) * $per_page); if ($remaining < $per_page) { $per_page = $remaining; } showPagination($page, $page_count, $range); echo '- ';
for ($i = $offset; $i < ($offset + $per_page); $i++) {
echo '
-
'.$xml->channel->item[$i]->title.'
'.excerpt($xml->channel->item[$i]->description, $words).' ';
}
echo "\n
More demos and snippets
Did you find this useful? There are more demos and code snippets this way.