Forum Moderators: coopster

Message Too Old, No Replies

PHP- First 10 words in a field

Output only the first 10 words...

         

Francis

6:28 am on Nov 2, 2004 (gmt 0)

10+ Year Member



Hello...

I know this is a simple procedure but how do I extract only the first 10 (or 20) words in a field named- DESCRIPTION.

Thanks.

mincklerstraat

8:34 am on Nov 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Assuming this is a field in a database, you'll be getting all your info from the db with mysql_fetch_row or mysql_fetch_assoc or such, which will return an array with the field names as the keys, and the respective values as values - probably in a loop something like

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)

$valuetoextract = explode(' ', $valuetoextract);
You'll get more 'exact' results if you use
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;
}

if by 'field' you mean a field in a form, this will come in as either
$_GET['DESCRIPTION']
or
$_POST['DESCRIPTION']
depending on whether you use the get or post method - and of course you won't be using a
while
loop 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.

dreamcatcher

9:50 am on Nov 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are pulling your data from a sql database, use SUBSTRING.

$query = mysql_query("SELECT SUBSTRING('DESCRIPTION', 0,10) FROM table");
$row = mysql_fetch_object($query);

echo $row->DESCRIPTION;

Oops! sorry, just saw you said words!

DaButcher

7:26 pm on Nov 2, 2004 (gmt 0)

10+ Year Member



For mysql:
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'];
}