Log in

View Full Version : RSS Feed Display - Word Limit



vonluck
08-01-2011, 02:00 PM
Managed to get some php written that will display a feed on my homepage. Works great, but I can only figure out how to limit the number of characters from a description, not words. Can anyone give me some pointers on including some code for a "number of words" limiter? Thanks!




<?php
$rss = new DOMDocument();
$rss->load(http://wordpress.org/news/);
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 3;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$description = substr($description, 0, 200);
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><font color=3c3c3c link=3c3c3c face=arial size=2><a href="'.$link.'" title="'.$title.'">'.$title.'</a></font><br />';
echo '<font color=3c3c3c face=arial size=1><em>Posted on '.$date.'</em></font><br />';
echo '<font color=3c3c3c face=arial size=2>'.$description.' ...</font></p>';
}
?>

JShor
08-01-2011, 04:58 PM
Interesting. Well, assuming all words are separated by a space " " you could explode the string with the space as the delimiter, which would insert the words into an array. Then you can just truncate the array to your limit.

Also, you have a syntax error at your second line because the URL is not contained as a string. I fixed that for you.

Try this:


<?php
$rss = new DOMDocument();
$rss->load("http://wordpress.org/news/");
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $desc,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 3;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$description = substr($description, 0, 200);

$limit_of_words = 20; // The number of words you want to limit by.
$description = explode(" ", $description);
$description = array_slice($description, 0, $limit_of_words);
$description = implode(" ", $description); // Glue the pieces back together.

$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><font color=3c3c3c link=3c3c3c face=arial size=2><a href="'.$link.'" title="'.$title.'">'.$title.'</a></font><br />';
echo '<font color=3c3c3c face=arial size=1><em>Posted on '.$date.'</em></font><br />';
echo '<font color=3c3c3c face=arial size=2>'.$description.' ...</font></p>';
}
?>

vonluck
08-01-2011, 06:03 PM
Ah, thanks. I actually got this to work with just a couple of lines.
Here's the final code. Makes $shortdesc but truncating the output of $description with trim, you can set the number of words - here it's 15.


<?php
$rss = new DOMDocument();
$rss->load('http://wordpress.org/news/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 3;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$description = substr($description, 0, 200);

$numwords = 15;
preg_match("/(\S+\s*){0,$numwords}/", $description, $regs);
$shortdesc = trim($regs[0]);

$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><font color=3c3c3c link=3c3c3c face=arial size=2><a href="'.$link.'" title="'.$title.'">'.$title.'</a></font><br />';
echo '<font color=3c3c3c face=arial size=1><em>Posted on '.$date.'</em></font><br />';
echo '<font color=3c3c3c face=arial size=2>'.$shortdesc.' ...</font></p>';
}
?>