Page 1 of 1
String functions for first 10 words
Posted: Mon Dec 21, 2009 9:15 am
by dimxasnewfrozen
I have a large database value as a string.
Is there a string function to display the first 10 words of the string as like a description?
Re: String functions for first 10 words
Posted: Mon Dec 21, 2009 9:44 am
by Reviresco
Code: Select all
$words = explode(' ', $string);
for($i=0; $i<10; $i++) {
echo $words[$i] . ' ';
}
or
Code: Select all
$words = explode(' ', $string);
$description = '';
for($i=0; $i<10; $i++) {
$description .= $words[$i] . ' ';
}
Re: String functions for first 10 words
Posted: Mon Dec 21, 2009 10:03 am
by dimxasnewfrozen
Oh awesome, I actually found this example:
Nice to know there's multiple ways to do this
Code: Select all
function wordlimit($string, $length = 5, $ellipsis = "...")
{
// Only display a description of the content in the news box.
$words = explode(' ', $string);
if (count($words) > $length)
return implode(' ', array_slice($words, 0, $length)) . $ellipsis;
else
return $string;
}
Re: String functions for first 10 words
Posted: Mon Dec 21, 2009 10:08 am
by pickle
wordwrap() might also be useful.