我有一张桌子,我希望限制可以显示的单词数量,如果可以的话可以显示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) . '…';
}
// 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";
}
?>
答案 0 :(得分: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) . '…';
}
// if our array is under the limit, just send it straight back
return $text;
}
这有点快速和肮脏 - 它不会破坏由&amp; nbsp;分隔的单词 - 但是你明白了。
答案 1 :(得分:1)
http://php.net/manual/en/function.str-word-count.php允许您将字符串分成一个单词数组。只需使用它并将前200个元素重新包含在一个字符串中......