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!

4 Answers 4

9
if (strlen($str) > 100) $str = substr($str, 0, 100) . "..."; 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0
function print_dots($message, $length = 100) { if(strlen($message) >= $length + 3) { $message = substr($message, 0, $length) . '...'; } echo $message; } print_dots($long_text); 

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.