Word count cut off
<p>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:</p>

<pre><code>      &lt;tbody&gt;
&lt;?php

        // table content:

        $i = 0;
        if ( is_array( $this-&gt;users ) &amp;&amp; count( $this-&gt;users ) &gt; 0 ) {
            foreach ( $this-&gt;users as $userIdx =&gt; $user) {
                $class = "sectiontableentry" . ( 1 + ( $i % 2 ) );      // evenodd class

                if ( $this-&gt;allow_profilelink ) {
                    $style = "style=\"cursor:hand;cursor:pointer;\"";
                    $style .= " id=\"cbU".$i."\"" ;
                } else {
                    $style = "";
                }
                if ( $user-&gt;banned ) {
                    echo "\t\t&lt;tr class=\"$class\"&gt;&lt;td colspan=\"".$colsNbr."\" &gt;&lt;span class=\"error\" style=\"color:red;\"&gt;"._UE_BANNEDUSER." ("._UE_VISIBLE_ONLY_MODERATOR.") :&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;\n";
                }
                echo "\t\t&lt;tr class=\"$class\" ".$style."&gt;\n";

                foreach ( array_keys( $this-&gt;columns ) as $colIdx ) {
                    echo "\t\t\t&lt;td valign=\"top\" class=\"cbUserListCol" . $colIdx . "\"&gt;" . $this-&gt;_getUserListCell( $this-&gt;tableContent[$userIdx][$colIdx] ) . "\t\t\t&lt;/td&gt;\n";
                }
                echo "\t\t&lt;/tr&gt;\n";
                $i++;
            }
        } else {
            echo "\t\t&lt;tr class=\"sectiontableentry1\"&gt;&lt;td colspan=\"".$colsNbr."\"&gt;"._UE_NO_USERS_IN_LIST."&lt;/td&gt;&lt;/tr&gt;\n";
        }
?&gt;
      &lt;/tbody&gt;
</code></pre>

<p>The column that I would like to limit the amount of words is class=cbUserListCol.</p>

<p>Cheers.</p>

<hr>

<p>Here is the updated version that still doesn't seem to limit the words:</p>

<pre><code>&lt;?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) &gt; $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) . '&amp;hellip;';
    }

    // if our array is under the limit, just send it straight back
    return $text;
}
        // table content:

        $i = 0;
        if ( is_array( $this-&gt;users ) &amp;&amp; count( $this-&gt;users ) &gt; 0 ) {
            foreach ( $this-&gt;users as $userIdx =&gt; $user) {
                $class = "sectiontableentry" . ( 1 + ( $i % 2 ) );      // evenodd class

                if ( $this-&gt;allow_profilelink ) {
                    $style = "style=\"cursor:hand;cursor:pointer;\"";
                    $style .= " id=\"cbU".$i."\"" ;
                } else {
                    $style = "";
                }
                if ( $user-&gt;banned ) {
                    echo "\t\t&lt;tr class=\"$class\"&gt;&lt;td colspan=\"".$colsNbr."\" &gt;&lt;span class=\"error\" style=\"color:red;\"&gt;"._UE_BANNEDUSER." ("._UE_VISIBLE_ONLY_MODERATOR.") :&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;\n";
                }
                echo "\t\t&lt;tr class=\"$class\" ".$style."&gt;\n";

                foreach ( array_keys( $this-&gt;columns ) as $colIdx ) {
                    echo "\t\t\t&lt;td valign=\"top\" class=\"cbUserListCol" . $colIdx . "\"&gt;" . ($this-&gt;_getUserListCell( $this-&gt;tableContent[$userIdx][$colIdx])). "\t\t\t&lt;/td&gt;\n";

                }
                echo "\t\t&lt;/tr&gt;\n";
                $i++;
            }
        } else {
            echo "\t\t&lt;tr class=\"sectiontableentry1\"&gt;&lt;td colspan=\"".$colsNbr."\"&gt;"._UE_NO_USERS_IN_LIST."&lt;/td&gt;&lt;/tr&gt;\n";
        }
?&gt;
</code></pre>
