2010-11-19 220 views
2

下表打印出整个评论(($rowquote["comment"]))。我怎么能限制它到250个字符?限制为250个字符

由于提前,

约翰

echo "<table class=\"samplesrec1quote\">"; 
while ($rowquote = mysql_fetch_array($resultquote)) { 
    echo '<tr>'; 
    echo '<td class="sitename1quote">"'.stripslashes($rowquote["comment"]).'"</td>'; 
echo '</tr>'; 
} 
echo "</table>"; 

编辑:我得到了它与left(comment, 250)查询工作。所以我猜jensgram应该得到他在别人的答案下的评论。

+0

[将多字节字符串截断为n个字符](http://stackoverflow.com/问题/ 2154220/truncate -a-multibyte-string-to-n-chars) - 包含截断字符串的解决方案,并且不考虑字边界 – Gordon 2010-11-19 09:27:20

+0

lol..4的完全相同的答案 – mpen 2010-11-19 09:27:29

回答

0
stripslashes(substr($rowquote["comment"],0,250) 
0
substr(stripslashes($rowquote["comment"]), 0, 250) 
0

使用substring

echo "<table class=\"samplesrec1quote\">"; 
while ($rowquote = mysql_fetch_array($resultquote)) { 
    echo '<tr>'; 
    echo '<td class="sitename1quote">"'.substr(stripslashes($rowquote["comment"]),0,250).'"</td>'; 
    echo '</tr>'; 
    } 
echo "</table>"; 
+0

why 225? – mpen 2010-11-19 09:27:09

+0

输入错误应该是250.谢谢反正 – 2010-11-19 09:28:00

1

substr(stripslashes($rowquote["comment"]), 0, 250)

2

substr(stripslashes($rowquote["comment"]),0,250)是最简单的方式,但功能,以确保它在空白结束是最好的,是这样的:

function sort_preview($the_content) 
{ 
    $display = 250; 
    $last = substr($the_content,$display,1); 
    if ($last != " ") 
    { 
    while ($last != " ") 
    { 
     $i=1; 
     $display = $display+$i; 
     $last = substr($the_content,$display,1); 
    } 
    } 
    $the_content = substr($the_content,0,$display); 
    } 
    return $the_content; 
    } 

调用类似sort_preview(stripslashes($ rowquote [“comment”]),0,250)

7

或者你可以在MySQL端做并保存一点带宽。

select substring(comment, 0, 250) as comment 

代替

select comment 
+0

谢谢。这是行得通的,但是它返回了最后250个字符。我怎样才能使第250个字符? – John 2010-11-19 09:55:29

+1

@John ['LEFT()'] (http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_left)可以工作。或者'SUBSTRING(comment,0,250)'。 – jensgram 2010-11-19 10:35:16

+0

对不起,我的错误,jensgram已经纠正我的mista柯。 – Surfrdan 2010-11-19 10:35:53

1

不是一个错过了一个趋势,我给它一个镜头,太:)

这将试图找到句点或空格削减在给定的容差范围内,如果在容差范围内存在多个合格切割长度($cutZone),则偏向较长的文本。

function cutstr($str, $length, $tolerance = 5) { 
    $cutZone = substr($str, $length - $tolerance, 2 * $tolerance); 
    $posPeriod = strrpos($cutZone, '.'); 
    $posSpace = strrpos($cutZone, ' '); 
    $ellipsis = '&hellip;'; 

    if (strlen($str) <= $length + $tolerance) { 
     // $str is shorter than $length+$tolerance - no cut, no ellipsis 
     $posCut = strlen($str); 
     $ellipsis = ''; 
    } elseif ($posPeriod !== false) { 
     // $str has period within tolerance - cut at period 
     $posCut = $length - $tolerance + $posPeriod; 
    } elseif ($posSpace !== false) { 
     // $str has space within tolerance - cut at space 
     $posCut = $length - $tolerance + $posSpace; 
    } else { 
     // Nothing to do - cut word 
     $posCut = $length; 
    } 

    return substr($str, 0, $posCut) . $ellipsis; 
} 

Demo Demo

一般而言,从未取从比所需要的DB更多的数据。您可以轻松从数据库中选择LEFT(<column>, <lengt+tolerance>),然后对该字符串执行剪切。


更新
偏袒较长的文本。

-1
echo "<table class=\"samplesrec1quote\">"; 
while ($rowquote = mysql_fetch_array($resultquote)) 
{ 
$temp=stripslashes($rowquote["comment"]); 
echo '<tr>'; 
echo '<td class="sitename1quote">'; 
for($loop=0;$loop<250;++$loop) 
    echo $temp[$loop]; 
echo '</td>'; 
echo '</tr>'; 
} 
echo "</table>"; 
+0

这种方法有什么问题??? – Kracekumar 2010-11-19 15:50:10

+0

我认为你得到的是倒数,因为它就像计算每个糖果数一个碗中的糖果袋数,然后估计有多少个会制作一个袋子,而不是只计算空袋子。 – dascandy 2011-07-28 12:18:12

-1

/*在cakephp中使用尾这样 *字符串::尾($ VAL [ '类别'] [ 'SHORT_DESCRIPTION'],250,阵列( '省略号'=> '', '精确' =>假)) */

((strlen的($ VAL [ '类别'] [ 'SHORT_DESCRIPTION'])> 250) ?$描述=字符串::尾($ VAL [ '类别'] [ 'short_description'],250,array('ellipsis'=>'','exact'=> false))。“...“ :$ description = $ val ['Category'] ['short_description'])。”...“;