【问题标题】:Limiting a comment to 250 characters将评论限制为 250 个字符
【发布时间】:2011-05-12 12:52:02
【问题描述】:

下表完整地打印出一条评论 (($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 应该得到他对下面其他人回答的评论的信任。

【问题讨论】:

标签: php mysql css


【解决方案1】:

/* 在 cakephp 中使用 tail 这样 * String::tail($val['Category']['short_description'],250, array('ellipsis' => '','exact' => false)) */

((strlen($val['Category']['short_description']) > 250) ? $description = String::tail($val['Category']['short_description'],250, array('ellipsis' => '','exact' => false))."..." : $description =$val['Category']['short_description']) ."...";

【讨论】:

    【解决方案2】:

    您也可以在 MySQL 端执行此操作并节省一点带宽。

    select substring(comment, 0, 250) as comment
    

    而不是

    select comment
    

    【讨论】:

    • 谢谢。这是有效的,但它正在返回最后 250 个字符。我怎样才能让它占据前 250 个?
    • @John LEFT() 会起作用。或者SUBSTRING(comment, 0, 250).
    • 如果您不想在其中存储更多数据,也可以将字段注释定义为 varchar(250)。
    • 我让它与 left(comment, 250) 一起使用。所以我猜 jensgram 应该因为他的回答而受到称赞。
    【解决方案3】:

    不要错过一个趋势,我也会试一试:)

    这将尝试在给定的容差范围内找到要剪切的句点或空格,如果在容差范围内存在多个合格的剪切长度($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;
    }
    

    (DemoDemo)

    一般来说,从不从数据库中获取比需要更多的数据。您可以轻松地从数据库中选择LEFT(&lt;column&gt;, &lt;lengt+tolerance&gt;),然后对该字符串执行切割。


    更新
    喜欢较长的文本。

    【讨论】:

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

      【讨论】:

      • 我认为您投了反对票,因为这就像通过计算单个糖果然后估计有多少可以组成一个袋子来计算碗中的糖果袋,而不是仅仅计算空袋子。跨度>
      【解决方案5】:

      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)

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

          为此使用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>";
          

          【讨论】:

          • 拼写错误应该是 250。无论如何谢谢。
          【解决方案8】:
          substr(stripslashes($rowquote["comment"]), 0, 250)
          

          【讨论】:

            【解决方案9】:
            stripslashes(substr($rowquote["comment"],0,250)
            

            【讨论】:

              猜你喜欢
              • 2010-11-04
              • 1970-01-01
              • 2011-08-06
              • 2017-12-02
              • 2012-09-17
              • 2012-11-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多