RE: [PHP] Truncate words in multiple paragraphs
| From: | php-mail | Date: | Fri, 03 Feb 2006 23:14:19 +0000 |
| Subject: | RE: [PHP] Truncate words in multiple paragraphs | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-229858@lists.php.net to get a copy of this message | ||
Nuts
Missed the need to preserve line breaks
Sorry
Dan
-----Original Message-----
From: tedd [mailto:tedd@sperling.com]
Sent: 03 February 2006 23:06
To: php-general@lists.php.net
Subject: Re: [PHP] Truncate words in multiple paragraphs
>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/
--
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