[DONE] Substr by words

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

 



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

[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