开发者

Limiting a comment to 250 characters

The table below prints out a comment (($rowquote["comment"]))in its entirety. How could I limit it to 250 开发者_运维百科characters?

Thanks in advance,

John

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

EDIT: I got it to work with left(comment, 250) in the query. So I guess jensgram should get credit fort the comment he made on someone else's answer below.


Alternatively you could do it at the MySQL end and save a little bandwidth.

select substring(comment, 0, 250) as comment

instead of

select comment


substr(stripslashes($rowquote["comment"]),0,250) is the simplest way, but a function to make sure it ends on a space is best, something like:

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;
    }

called like sort_preview(stripslashes($rowquote["comment"]),0,250)


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


Not one to miss out on a trend, I'll give it a shot, too :)

This will try and find periods or spaces to cut at within a given tolerance, favoring longer texts if more than one qualified cut length exists within tolerance ($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)

In general, never fetch more data from the DB than needed. You can easily just select LEFT(<column>, <lengt+tolerance>) from the DB and then perform the cutting on this string.


Update
Favoring longer texts.


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


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


use substring for this

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>";


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>";


/* in cakephp use tail like this * 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']) ."...";

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜