0

I have a PHP loop that adds data into a table cell. However, I want to apply a static size to the table cell, so if more data is returned than can fit inside the cell I want the excess characters to be cut off and end with a "..."

For example, one data entry has 270 characters, but only the first 100 are displayed in the table cell. Follow by a "..."

Any ideas on how to do this?

Thanks!

user547794
  • 14,263
  • 36
  • 103
  • 152

4 Answers4

9
if (strlen($str) > 100) $str = substr($str, 0, 100) . "...";
Burak Guzel
  • 1,267
  • 1
  • 12
  • 12
1

You can use mb_strimwidth

printf('<td>%s</td>', mb_strimwidth($cellContent, 0, 100, '…'));

If you want to truncate with respect to word boundaries, see

You can also control content display with the CSS property text-overflow: ellipsis

Unfortunately, browser support varies.

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
0
function print_dots($message, $length = 100) {
  if(strlen($message) >= $length + 3) {
    $message = substr($message, 0, $length) . '...';
  }

  echo $message;
}

print_dots($long_text);
Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
0
$table_cell_data = "";  // This would hold the data in the cell
$cell_limit      = 100; // This would be the limit of characters you wanted

// Check if table cell data is greater than the limit
if(strlen($table_cell_data) > $cell_limit) {
   // this is to keep the character limit to 100 instead of 103. OPTIONAL
   $sub_string = $cell_limit - 3; 

   // Take the sub string and append the ...
   $table_cell_data = substr($table_cell_data,0,$sub_string)."...";
}

// Testing output
echo $table_cell_data."<br />\n";
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383