1

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) . '&hellip;';
    }

    // 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 ) {
            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";
        }
?>
badger013
  • 11
  • 1
  • 3
  • This is called truncating. There is quite a bit about using php to do so out there, including here on stackoverflow (http://stackoverflow.com/questions/965235/how-can-i-truncate-a-string-in-php). If you want '...' on the end, you can write your function to check if something does in fact need to be truncated, and if it does, simply add the string '...' to the end. Preferably add it as …, which is the proper html entity for an elipsis. – Steve Adams Jul 06 '11 at 23:02
  • Sorry for the double comment; here is a useful link that helped me when I first had this question: http://www.the-art-of-web.com/php/truncate/ – Steve Adams Jul 06 '11 at 23:08
  • possible duplicate of [How to display a snippet from a BIG string in php?](http://stackoverflow.com/questions/6582994/how-to-display-a-snippet-from-a-big-string-in-php) – KingCrunch Jul 06 '11 at 23:51

2 Answers2

2
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) . '&hellip;';
    }

    // 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 &nbsp;, for example - but you get the idea.

kabuko
  • 36,028
  • 10
  • 80
  • 93
Mathew
  • 8,203
  • 6
  • 37
  • 59
  • The problem with this simple of a truncation function is that it may cut off parts of words. – Steve Adams Jul 06 '11 at 23:05
  • Thanks for the quick reply, but I can't quite get it to work, could you quickly post where I should input the code... – badger013 Jul 06 '11 at 23:26
  • Not sure exactly what _getUserListCell or tableContent do, but calling truncateWords($this->_getUserListCell( $this->tableContent[$userIdx][$colIdx])) should do it. – Mathew Jul 06 '11 at 23:31
  • Also, explode(array(' '), $text); should read explode(' ', $text); \*blush\* – Mathew Jul 06 '11 at 23:33
  • Sorry to keep asking, here is my code that doesn't seem to be getting limited to 200. – badger013 Jul 07 '11 at 00:01
  • for this one sorry: foreach ( array_keys( $this->columns ) as $colIdx ) { echo "\t\t\t" . ($this->_getUserListCell( $this->tableContent[$userIdx][$colIdx])). "\t\t\t\n"; – badger013 Jul 07 '11 at 00:11
1

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...

iHaveacomputer
  • 1,427
  • 4
  • 14
  • 30