0

I am pulling some data from a mysql database and building a table with it. I have a truncation function to chop down very long strings.

Here is the truncation function:

    function myTruncate($string, $limit, $break=".", $pad="...")
    {
      // return with no change if string is shorter than $limit
      if(strlen($string) <= $limit) return $string;

      // is $break present between $limit and the end of the string?
      if(false !== ($breakpoint = strpos($string, $break, $limit))) {
        if($breakpoint < strlen($string) - 1) {
          $string = substr($string, 0, $breakpoint) . $pad;
        }
      }
      return $string;
    }

Here is the table building bit where the function is called:

    while($row = mysql_fetch_assoc($liveItemQuery)){
       //get donation type
       $content .= "<tr><td>" . $i . "</td><td>" . $row['timestamp'] . "</td><td>" . myTruncate($row['description'], 25) . "</td><td>" . $row['po_number'] . "</td><td>" . number_format($row['amount'], 2) . "</td><td><a href=\"?view=expenses&itemid=" . $row['id'] . "\">Edit</a></td></tr>";
       $i++;
   }

That being said, truncation is not being performed on the description field. It may be worth noting that description is stored as datatype text in the database. Also, a var dump of myTruncate($row['description'], 25) reports string(1995), the full length of the text.

So what is going on?

Thank you in advance for any help.

lampwins
  • 920
  • 1
  • 9
  • 25

1 Answers1

0

"Truncate no matter what". Much easier.

function myTruncate($s, $len){
    return substr($s, 0, $len) . (strlen($s)>$len? '...':'');
}
Popnoodles
  • 28,090
  • 2
  • 45
  • 53