[thelist] PHP show first 10 words of MySQL field

Wesley Mason wes at pmason.karoo.co.uk
Thu Aug 28 07:12:03 CDT 2003


Try:
http://www.evilwalrus.com/viewcode.php?codeEx=427

-
Wes
(from work)


Matt King wrote:
>>I have now got it working, and very simple it is to;
>>
>>$rest = substr("$myrow[text]", 0, 180);
>>      echo("$rest");
>>
>>Many thanks, Austin
> 
> 
> But that only shows the first 180 characters.  Wouldn't it look funny if
> you cut off a word at the end of this string?
> 
> Here's a more elegant (but more resource taxing) solution:
> 
> $WordLimit = 20;
> $Words = explode(" ",$String,$WordLimit);
> array_pop($Words);  // Last element contains remaining text, we don't need it
> 
> foreach ($Words as $Word)
> {
>   echo $Word . " ";
> }
> 




More information about the thelist mailing list