Log in

View Full Version : Cut off a given string automatically?



rctxtreme
07-16-2007, 12:12 AM
OK, if you view this RSS feed (http://gmnews.wordpress.com/feed/) in Firefox 2+, you would've noticed that descriptions get cut off automatically. Unfortunately, when something like an RSS parser in PHP reads the RSS feed, the description is the entire blog post.

So is it possible you could have something like this where feeding a function or something a variable containing a long string, and just cutting it off after a specified number of words, then adding [...]. Thanks!

Twey
07-16-2007, 01:04 AM
function abbreviate($paragraph, $length = 30, $final = '...') {
$words = explode(' ', $paragraph);
if(count($words) <= $length)
return $paragraph;
else
return implode(' ', array_slice($words, 0, $length + 1)) . $final;
}

rctxtreme
07-16-2007, 01:37 AM
Now this is much better than the first thing I found. Thanks a lot =D