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?
String functions for first 10 words
Moderator: General Moderators
-
dimxasnewfrozen
- Forum Commoner
- Posts: 84
- Joined: Fri Oct 30, 2009 1:21 pm
Re: String functions for first 10 words
Code: Select all
$words = explode(' ', $string);
for($i=0; $i<10; $i++) {
echo $words[$i] . ' ';
}Code: Select all
$words = explode(' ', $string);
$description = '';
for($i=0; $i<10; $i++) {
$description .= $words[$i] . ' ';
}-
dimxasnewfrozen
- Forum Commoner
- Posts: 84
- Joined: Fri Oct 30, 2009 1:21 pm
Re: String functions for first 10 words
Oh awesome, I actually found this example:
Nice to know there's multiple ways to do this
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
wordwrap() might also be useful.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.