Word count cut off
I have a table which I am wanting to restrict the amount of words that can be displayed, after 200 words if it could go ... that would be great, but that is just an added bonus really. Here is the code that gathers the content from the database:
<tbody>
<?php
// table content:
$i = 0;
if ( is_array( $this->users ) && count( $this->users ) > 0 ) {
foreach ( $this->users as $userIdx => $user) {
$class = "sectiontableentry" . ( 1 + ( $i % 2 ) ); // evenodd class
if ( $this->allow_profilelink ) {
$style = "style=\"cursor:hand;cursor:pointer;\"";
$style .= " id=\"cbU".$i."\"" ;
} else {
$style = "";
}
if ( $user->banned ) {
echo "\t\t<tr class=\"$class\"><td colspan=\"".$colsNbr."\" ><span class=\"error\" style=\"color:red;\">"._UE_BANNEDUSER." ("._UE_VISIBLE_ONLY_MODERATOR.") :</span></td></tr>\n";
}
echo "\t\t<tr class=\"$class\" ".$style.">\n";
foreach ( array_keys( $this->columns ) as $colIdx ) {
echo "\t\t\t<td valign=\"top\" class=\"cbUserListCol" . $colIdx . "\">" . $this->_getUserListCell( $this->tableContent[$userIdx][$colIdx] ) . "\t\t\t</td>\n";
}
echo "\t\t</tr>\n";
$i++;
}
} else {
echo "\t\t<tr class=\"sectiontableentry1\"><td colspan=\"".$colsNbr."\">"._UE_NO_USERS_IN_LIST."</td></tr>\n";
}
?>
</tbody>
The column that I would like to limit the amount of words is class=cbUserListCol.
Cheers.
Here is the updated version that still doesn't seem to limit the words:
<?php
function truncateWords($text, $maxLength = 200)
{
// explode the text into an array of words
$wordArray = explode(' ', $text);
// do we have too many?
if( sizeof($wordArray) > $maxLength )
{
// remove the unwanted words
$wordArray = array_slice($wordArray, 0, $maxlength);
// turn the word array back into a string and add our ...
return implode(' ', $wordArray) . '…';
}
// if our array is under the limit, just send it straight back
return $text;
}
// table content:
$i = 0;
if ( is_array( $this->users ) && count( $this->users ) > 0 ) {
开发者_StackOverflow中文版 foreach ( $this->users as $userIdx => $user) {
$class = "sectiontableentry" . ( 1 + ( $i % 2 ) ); // evenodd class
if ( $this->allow_profilelink ) {
$style = "style=\"cursor:hand;cursor:pointer;\"";
$style .= " id=\"cbU".$i."\"" ;
} else {
$style = "";
}
if ( $user->banned ) {
echo "\t\t<tr class=\"$class\"><td colspan=\"".$colsNbr."\" ><span class=\"error\" style=\"color:red;\">"._UE_BANNEDUSER." ("._UE_VISIBLE_ONLY_MODERATOR.") :</span></td></tr>\n";
}
echo "\t\t<tr class=\"$class\" ".$style.">\n";
foreach ( array_keys( $this->columns ) as $colIdx ) {
echo "\t\t\t<td valign=\"top\" class=\"cbUserListCol" . $colIdx . "\">" . ($this->_getUserListCell( $this->tableContent[$userIdx][$colIdx])). "\t\t\t</td>\n";
}
echo "\t\t</tr>\n";
$i++;
}
} else {
echo "\t\t<tr class=\"sectiontableentry1\"><td colspan=\"".$colsNbr."\">"._UE_NO_USERS_IN_LIST."</td></tr>\n";
}
?>
function truncateWords($text, $maxLength = 200)
{
// explode the text into an array of words
$wordArray = explode(' ', $text);
// do we have too many?
if( sizeof($wordArray) > $maxLength )
{
// remove the unwanted words
$wordArray = array_slice($wordArray, 0, $maxLength);
// turn the word array back into a string and add our ...
return implode(' ', $wordArray) . '…';
}
// if our array is under the limit, just send it straight back
return $text;
}
This is a bit quick and dirty - it won't break words separated by , for example - but you get the idea.
http://php.net/manual/en/function.str-word-count.php lets you divide a string into an array of words. Just use that and implode the first 200 elements back into a string...
精彩评论