Good start guys. That's usually how I start down the path in solving a new problem. Invariably down the road I find a more "refined" way of doing what I did the hard way. The hard way is good though, you learn stuff along the way. Let's see how tight we can make this though: <?php $string = " This is a test string of some sort. "; $truncated = truncstr($string); echo $truncated; function truncstr($tmpstr) { $maxwords = 4; $tmpstr = trim($tmpstr); $wordarr = explode(" ", $tmpstr); if (count($wordarr) > $maxwords) { $wordarr = array_slice($wordarr, 0, $maxwords); $tmpstr = implode(" ", $wordarr) . "..."; } else { $tmpstr = implode(" ", $wordarr); } return $tmpstr; } ?> .... Or if you want really obfuscated code... <?PHP $string = " This is a test string of some sort. "; $truncated = truncstr($string); echo $truncated; function truncstr($tmpstr) { return implode(" ", array_slice(explode(" ", trim($tmpstr)), 0, 4)) . "..."; } ?> -TG = = = Original message = = = Good job! However, let me give a few suggestions to optimize the code function trim_text($text, $count) ~$text = str_replace(" ", " ", $text); ~/* ~ * This is redundant; you are replacing all " " in $text with " " ~ * maybe you meant ~ * $text = trim($text); ? ~ */ ~$string = explode(" ", $text); ~ ~/* ~ * For better programming practice, you should initialize $trimed ~ * I believe if you turn on error_reporting for all in php.ini ~ * PHP will display all warnings and errors. ~ */ ~for ( $wordCounter = 0; $wordCounter <= $count;wordCounter++ ) ~/* ~ * Typo - you forgot the $ for wordCounter++ ~ * ~ * for ( $wordCounter = 0; $wordCounter <= $count; $wordCounter++) ~ */ ~ ~~$trimed .= $string[$wordCounter]; ~~if ( $wordCounter < $count ) ~~ ~~~$trimed .= " "; ~~ ~~else ~~ ~~~$trimed .= "..."; ~~ ~ ~$trimed = trim($trimed); ~return $trimed; This is purely my suggestion... and I'm not saying it is better... but if I were you, I'd do it this way : function trim_text($text, $count) ~$text = trim($text); ~$string = explode(" ", $text); ~$trimed=''; ~for ( $wordCounter = 0; $wordCounter <= $count; $wordCounter++ ) ~ ~~$trimed .= $string[$wordCounter].' '; ~ ~$trimed = trim($trimed); ~if (count($string)>$count) ~~$trimed.='...'; ~return $trimed; The only difference(not that you'd notice in a lightweight routine like this) is that, I don't have that if-else block inside the loop. Danny wrote: >Finally i found it (Google is god, you only have to ask the right question) > function trim_text($text, $count) >$text = str_replace(" ", " ", $text); >$string = explode(" ", $text); >for ( $wordCounter = 0; $wordCounter <= $count;wordCounter++ ) >$trimed .= $string[$wordCounter]; >if ( $wordCounter < $count ) $trimed .= " "; >else $trimed .= "..."; > >$trimed = trim($trimed); >return $trimed; > > >Usage >------------------------------------------------ >$string = "one two three four"; >echo trim_text($string, 3); > > >---------- Forwarded message ---------- >From: Danny <metalito@xxxxxxxxx> >Date: Oct 29, 2005 1:36 PM >Subject: Substr by words >To: php-general@xxxxxxxxxxxxx > > Hi, > I need to extract 50 words more or less from a description field. How can i >do that?. Substr, cuts the words. Is there any other way to that, without >using and array? I mean and implemented function in PHP 4.x > I~ve been googling around, but wordwrap, and substr is driving me mad... > Thanks in advance >Best Regards > >-- >dpc > > >-- >dpc ___________________________________________________________ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php