Limiting number of characters displayed in table cell

后端 未结 4 339
渐次进展
渐次进展 2021-01-27 11:48

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 th

相关标签:
4条回答
  • 2021-01-27 12:10
    function print_dots($message, $length = 100) {
      if(strlen($message) >= $length + 3) {
        $message = substr($message, 0, $length) . '...';
      }
    
      echo $message;
    }
    
    print_dots($long_text);
    
    0 讨论(0)
  • 2021-01-27 12:16
    $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";
    
    0 讨论(0)
  • 2021-01-27 12:19
    if (strlen($str) > 100) $str = substr($str, 0, 100) . "...";
    
    0 讨论(0)
  • 2021-01-27 12:28

    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

    • Truncate a multibyte String to n chars

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

    • http://www.quirksmode.org/css/textoverflow.html

    Unfortunately, browser support varies.

    0 讨论(0)
提交回复
热议问题