Would this work? <? function trunc($string, $limit = 11) { $arr = array(); $arr = (preg_split('/\s/', preg_replace('/(\s)\s+/', '\\1',$string))); return implode(' ', array_slice($arr, 0, $limit)); } $str = "This is my\n\r\n\ntest string\nIsn't it nice?\r\nI quite like it\n\nHow about you?"; echo trunk($str); ?> This returns: This is my test string Isn't it nice? I quite like Of course, the return doesn't preserve line breaks :) HTH Dan -------- http://chrome.me.uk -----Original Message----- From: Richard Lynch [mailto:ceo@xxxxxxxxx] Sent: 03 February 2006 21:45 To: Verdon Vaillancourt Cc: ceo@xxxxxxxxx; John Meyer; PHP eMail List Subject: Re: Truncate words in multiple paragraphs Just do this: $string = str_replace("\n", "<br>", $string); $string = trim_text($string); $string = str_replace("<br>", "\n", $string); Actually <br> could be any sequence of non-whitespace characters unlikely to be in the text in the first place. On Fri, February 3, 2006 3:29 pm, Verdon Vaillancourt wrote: > Hi Richard, > > I quickly realized this and fooled with a few variations. Also have to > take into account that the <br> or the <br /> get counted as 1 or 2 > words respectively, and so the trim can end up in the wrong place. > I've > tried fiddling with counting the number of br's in the string and > adding that to the truncation limit before the preg_split, but this is > still not ideal. > > I'm going to have a look at your other suggestion too. > > Thanks for your thoughts :) > verdon > > On 3-Feb-06, at 4:21 PM, Richard Lynch wrote: > >> >> >> This would work if you replace the <br /> with <br> -- Otherwise, >> it's >> too likely to break up "<br />" in the trim_text function and then >> you >> end up with: >> >> "start of string ... almost 255 words, blah blah blah<br" >> >> On Fri, February 3, 2006 11:25 am, John Meyer wrote: >>> -----BEGIN PGP SIGNED MESSAGE----- >>> Hash: SHA1 >>> >>> Verdon Vaillancourt wrote: >>>> Hi :) >>>> >>>> I am using the following function (that I found in the user >>>> comments >>>> on >>>> php.net manual) for trimming the number of words accepted via a >>>> form >>>> field >>>> >>>> // Truncation is by word limit, not character limit. So if you >>>> limit >>>> // to 255, then it will cut off a text item to 255 words, not 255 >>>> characters >>>> function trim_text ($string, $truncation=250) { >>>> $string = preg_split("/\s+/",$string,($truncation+1)); >>>> unset($string[(sizeof($string)-1)]); >>>> return implode(' ',$string); >>>> } >>> >>> >>> How about >>> $string = nl2br($string); >>> $string = trim_text($string); >>> $string = str_replace("<br />","\n",$string); >>> -----BEGIN PGP SIGNATURE----- >>> Version: GnuPG v1.4.2 (GNU/Linux) >>> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org >>> >>> iD8DBQFD45Hzj60GAoLuoDkRAjSqAKCxvGlJmSCVHozWBDjjZnKMEZOfSwCfenVj >>> lRSChtsMRqRnOYdZpk5YQ0c= >>> =Dnly >>> -----END PGP SIGNATURE----- >>> >>> -- >>> PHP General Mailing List (http://www.php.net/) >>> To unsubscribe, visit: http://www.php.net/unsub.php >>> >>> >> >> >> -- >> Like Music? >> http://l-i-e.com/artists.htm >> >> >> >> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php __________ NOD32 1.1393 (20060203) Information __________ This message was checked by NOD32 antivirus system. http://www.eset.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php