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(' & ', ' & ', $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>';
}
?>
<?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(' & ', ' & ', $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>';
}
?>