将评论限制为250个字符

时间:2010-11-19 09:21:56

标签: php mysql css

下表完整地打印出评论(($rowquote["comment"]))。我怎么能把它限制在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应该对他在下面的别人的回答中做出的评论有所了解。

9 个答案:

答案 0 :(得分:7)

或者你可以在MySQL端完成它并节省一点带宽。

select substring(comment, 0, 250) as comment

而不是

select comment

答案 1 :(得分: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)

答案 2 :(得分:1)

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

答案 3 :(得分: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

通常,从不从数据库获取的数据超过了所需数据。您可以轻松地从数据库中选择LEFT(<column>, <lengt+tolerance>),然后对此字符串执行剪切。


<强>更新
偏爱较长的文本。

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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

答案 6 :(得分: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>";

答案 7 :(得分:-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>";

答案 8 :(得分:-1)

cakephp中的

/ *使用这样的尾巴 * String :: tail($ val [&#39; Category&#39;] [&#39; short_description&#39;],250,array(&#39;省略号&#39; =&gt;&#39;&# 39;,&#39; exact&#39; =&gt; false)) * /

((strlen($ val [&#39;类别&#39;] [&#39; short_description&#39;])&gt; 250)     ? $ description = String :: tail($ val [&#39; Category&#39;] [&#39; short_description&#39;],250,array(&#39;省略号&#39; =&gt;&#39; &#39;,&#39;确切&#39; =&gt; false))。&#34; ...&#34;     :$ description = $ val [&#39;类别&#39;] [&#39; short_description&#39;])。&#34; ...&#34;;