Forum Moderators: coopster
while $row = (mysql_fetch_row($result)){
$valuetoextract = $row['DESCRIPTION'];
Then you can split this up into an array with each word as an element of an array - splitting it up at every occurrence of the character ' ' (space character) You'll get more 'exact' results if you use
$valuetoextract = explode(' ', $valuetoextract);
preg_split()(like you can make it 'skip over' double spaces, and get fancy in stipulating stuff) but you probably aren't ready for regexes yet. Anyways, now $valuetoextract is an array, with each word as an element, starting from the first word. We just have to 'cut' it off at 10 (or 20) and then change it back to a string.
$valuetoextract = arrayslice($valuetoextract, 0, 10); /* chop off unwanted portion 'or 20' - replace 10 here with 20 - if this gives you an error when it's less than 10, do it conditionally checking count($valuetoextract) */
$valuetoextract = implode(' ', $valuetoextract);
/* change the array back to a string */
echo $valuetoextract;
}
$_GET['DESCRIPTION']or
$_POST['DESCRIPTION']depending on whether you use the get or post method - and of course you won't be using a
whileloop there.
If you don't mind having your string length being determined by the number of characters instead of the number of words, you could shorten the code above by using wordwrap()
[be2.php.net] properly with similar code.
LEFT(str,len)
eg. you can select a left(field, 10) AS short_desc;
SELECT description, left(description, 10) AS short_desc FROM yourtable;
You also have a right(str,len) function.
[dev.mysql.com...]
A way to do it in php:
if (strlen($row['description']) > 10) // if len is more than 10
{ // shorten it and add trailing dots.
$description = substr($row['description'], 0, 10) . "...";
}
else // len is less than 10, use original description.
{
$description = $row['description'];
}