【问题标题】:Word count cut off字数被切断
【发布时间】:2013-01-10 03:51:40
【问题描述】:

我有一个表格,我想限制可以显示的字数,如果可以的话,在 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";
        }
?>

【问题讨论】:

标签: php


【解决方案1】:
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;
}

这有点快速和肮脏 - 例如,它不会破坏由   分隔的单词 - 但你明白了。

【讨论】:

  • 这个简单的截断函数的问题是它可能会截断部分单词。
  • 感谢您的快速回复,但我无法使其正常工作,您能否快速发布我应该输入代码的位置...
  • 不确定 _getUserListCell 或 tableContent 究竟做了什么,但调用 truncateWords($this->_getUserListCell( $this->tableContent[$userIdx][$colIdx])) 应该可以做到。
  • 另外,explode(array(' '), $text);应该读为explode(' ', $text); *脸红*
  • 抱歉一直问,这是我的代码,似乎并没有被限制为 200。
【解决方案2】:

http://php.net/manual/en/function.str-word-count.php 允许您将字符串划分为单词数组。只需使用它并将前 200 个元素内爆回一个字符串...

【讨论】:

  • 为什么我以前从未见过这个函数?!谢谢:)
猜你喜欢
  • 2011-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-13
  • 2017-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多