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