[thelist] PHP show first 10 words of MySQL field

Matt King evolt at magnetinternet.com
Thu Aug 28 00:56:20 CDT 2003


> 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 . " ";
}

-- 
Matt King
magnet | internet
http://www.magnetinternet.com



More information about the thelist mailing list