Php 字数截止

Php 字数截止,php,Php,我有一个表,我想限制的字数,可以显示,200字后,如果它可以去。。。那太好了,但这只是一个额外的好处。以下是从数据库收集内容的代码: <tbody> <?php // table content: $i = 0; if ( is_array( $this->users ) && count( $this->users ) > 0 ) { foreach (

我有一个表,我想限制的字数,可以显示,200字后,如果它可以去。。。那太好了,但这只是一个额外的好处。以下是从数据库收集内容的代码:

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

我想限制字数的列是class=cbUserListCol

干杯


以下是更新版本,似乎仍然没有限制文字:

<?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";
        }
?>


这有点快,也有点脏——例如,它不会打断以分隔的单词,但你明白了。

让你把一个字符串分割成一个单词数组。只需使用它,并将前200个元素内爆回一个字符串…

这称为截断。这里有很多关于使用php来实现这一点的内容,包括stackoverflow()。如果希望在末尾加上“…”,可以编写函数来检查是否确实需要截断某些内容,如果需要,只需在末尾加上字符串“…”。最好将其添加为&hellip;,这是省略号的正确html实体;当我第一次问这个问题时,这里有一个有用的链接帮助了我:这个简单的截断函数可能会重复这个问题,因为它可能会截断部分单词。谢谢你的快速回复,但我无法让它正常工作,你能不能快速发布我应该在哪里输入代码…不确定_getUserListCell或tableContent到底做什么,但是调用truncateWords($this->_getUserListCell($this->tableContent[$userIdx][$colIdx])应该做什么;应读为explode(“”,$text)*脸红*很抱歉一直问,这是我的代码,似乎没有限制在200。为什么我以前从未见过这个函数?!谢谢:)
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;
}