Re: Truncate words in multiple paragraphs

From: Date: Fri, 03 Feb 2006 23:05:43 +0000
Subject: Re: Truncate words in multiple paragraphs
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-229856@lists.php.net to get a copy of this message
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); } This works well, except it is also removing any line breaks. For instance... -snip- I'd like to (need to) retain the line breaks. Any suggestions? Thanks, verdon
verdon: I had the same problem and fixed it with this: // remove all linefeeds and replace with new paragraph $pattern = "\n"; $replace = "</p><p>"; $des = eregi_replace($pattern, $replace, $des); Then in your html, use: <p><?php echo($des); ?></p> Worked for me. tedd -- -------------------------------------------------------------------------------- http://sperling.com/

« previous php.general (#229856) next »