Re: Truncate words in multiple paragraphs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux