1. Why is this simple script not working?
For starters, if you want it to loop, switch the > to < in your loop. You're terminating the condition right off the bat, so it's not looping.
2. Is there a better way of doing this?
I use this function. Here's the function with an example:
[php]
<?
// Get the first $wordcount words from a sentence - used for long descriptions.
function get_words($str, $wordcount) {
$words=preg_split('/([\s.,;]+)/',$str,$wordcount+1,PREG_SPLIT_DELIM_CAPTURE);
array_pop($words);
return(implode('',$words));
}
$data = "this is a piece of text yeah baby yeah!";
$newdata = get_words($data, 4);
echo $newdata;
?>
[/php]