2

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.

Community
  • 1
  • 1
John
  • 4,820
  • 21
  • 62
  • 92
  • possible duplicate of [Truncate a multibyte String to n chars](http://stackoverflow.com/questions/2154220/truncate-a-multibyte-string-to-n-chars) - contains solutions for truncating string with and without respecting word boundaries. – Gordon Nov 19 '10 at 09:27
  • lol..4 of the exact same answer. – mpen Nov 19 '10 at 09:27

9 Answers9

7

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
jensgram
  • 31,109
  • 6
  • 81
  • 98
Surfrdan
  • 505
  • 3
  • 12
  • Thanks. This is working, but it is returning the last 250 characters. How can I make it take the first 250? – John Nov 19 '10 at 09:55
  • 1
    @John [`LEFT()`](http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_left) will work. Alternatively `SUBSTRING(comment, 0, 250)`. – jensgram Nov 19 '10 at 10:35
  • Or just define the field comment as varchar(250), if you don't intent to store more data in it. – JochenJung Nov 19 '10 at 11:38
  • I got it to work with left(comment, 250). So I guess jensgram should get credit fort his answer. – John Nov 20 '10 at 09:50
2

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)

Liam Bailey
  • 5,879
  • 3
  • 34
  • 46
1

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

Ross
  • 18,117
  • 7
  • 44
  • 64
1

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.

jensgram
  • 31,109
  • 6
  • 81
  • 98
0
stripslashes(substr($rowquote["comment"],0,250)
Vadyus
  • 1,299
  • 8
  • 19
0
substr(stripslashes($rowquote["comment"]), 0, 250)
heximal
  • 10,327
  • 5
  • 46
  • 69
0

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>";
Chinmayee G
  • 7,947
  • 2
  • 31
  • 41
-1

/* 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']) ."...";

-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>";
Kracekumar
  • 19,457
  • 10
  • 47
  • 56
  • I think you got the downvote because it's like counting bags of candy in a bowl by counting the individual candies & then estimating how many would make a bag, as opposed to just counting the empty bags. – dascandy Jul 28 '11 at 12:18